Pycharm visual warning about unresolved attribute reference(Pycharm 关于未解析属性引用的视觉警告)
问题描述
我有两个如下所示的类:
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)
继承必须是这样的;第二类包含 THE_DCT
和第一类包含 def the_dct
.
Inheritance has to be this way; second class containing THE_DCT
and first class containing def the_dct
.
它工作得很好,但我的问题是我在 Pycharm(未解析的属性引用)中收到关于 BaseClass
中的 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
.
- 它警告我有什么原因(比如为什么我应该避免它)?
- 有什么我应该做的不同的事情吗?
推荐答案
在 BaseClass
中你引用 self.THE_DCT
,但是当 PyCharm 查看这个类时,它看到THE_DCT
不存在.
Within BaseClass
you reference self.THE_DCT
, yet when PyCharm looks at this class, it sees that THE_DCT
doesn't exist.
假设您将其视为抽象类,PyCharm 不知道这是您的意图.它所看到的只是一个访问属性的类,该属性不存在,因此会显示警告.
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.
虽然你的代码运行得非常好(只要你从不实例化BaseClass
),你真的应该把它改成:
Although your code will run perfectly fine (as long as you never instantiate BaseClass
), you should really change it to:
class BaseClass(object):
THE_DCT = {}
def the_dct(self):
return self.THE_DCT
这篇关于Pycharm 关于未解析属性引用的视觉警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Pycharm 关于未解析属性引用的视觉警告


基础教程推荐
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 筛选NumPy数组 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01