关于未解析属性引用的视觉警告

关于未解析属性引用的视觉警告

本文介绍了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 关于未解析属性引用的视觉警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 01:42