Python 关于未解析的属性引用的 Pycharm 视觉警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28172008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pycharm visual warning about unresolved attribute reference
提问by Fermi paradox
I have two classes that look like this:
我有两个看起来像这样的类:
class BaseClass(object):
def the_dct(self):
return self.THE_DCT
class Kid(BaseClass):
THE_DCT = {'vars': 'values'}
# Code i ll be running
inst = Kid()
print(inst.the_dct)
Inheritance has to be this way; second class containing THE_DCT
and first class containing def the_dct
.
继承必须是这样;第二类包含THE_DCT
和第一类包含def the_dct
.
It works just fine, but my problem is that i get a warning in Pycharm (unresolved attribute reference), about THE_DCT
in BaseClass
.
它工作得很好,但我的问题是我在 Pycharm(未解析的属性引用)中收到警告,关于THE_DCT
in BaseClass
.
- Is there a reason why it's warning me (as in why i should avoid it)?
- Is there something i should do differently?
- 是否有理由警告我(例如为什么我应该避免它)?
- 有什么我应该做的不同吗?
采纳答案by dursk
Within BaseClass
you reference self.THE_DCT
, yet when PyCharm looks at this class, it sees that THE_DCT
doesn't exist.
在BaseClass
您的引用中self.THE_DCT
,但是当 PyCharm 查看这个类时,它发现它THE_DCT
不存在。
Assuming you are treating this as an Abstract Class, PyCharm doesn't know that that is your intention. All it sees is a class accessing an attribute, which doesn't exist, and therefore it displays the warning.
假设您将其视为抽象类,PyCharm 不知道这是您的意图。它所看到的只是一个访问属性的类,该属性不存在,因此它会显示警告。
Although your code will run perfectly fine (as long as you never instantiate BaseClass
), you should really change it to:
尽管您的代码将运行得非常好(只要您从未实例化BaseClass
),您确实应该将其更改为:
class BaseClass(object):
THE_DCT = {}
def the_dct(self):
return self.THE_DCT