PyCharm 警告 dict 生成的属性未解析的属性 [英] PyCharm warns about unresolved attribute for dict generated attributes
问题描述
考虑以下代码:
class SomeClass:
def __init__(self, config):
for key, value in config.items():
self.__setattr__(key, value)
def foo(self):
print(self.a)
PyCharm 2017.1.1 警告我未解析的属性 a
,我完全同意.但我不想在构造函数中写一大堆属性.
PyCharm 2017.1.1 warns me about unresolved attribute a
and I absolutely agree with it. But I don't want to write a blob of attributes in the constructor.
是否有任何解决方法可以在没有警告的情况下编写相同的功能?或者至少,我如何通知 PyCharm 不要警告我这个特定类的属性?
Is there any workaround to write the same functionality without warning? Or at least, how can I notify PyCharm not to warn me about attributes of this particular class?
推荐答案
你可以这样做来取悦它:
You can do this to please it:
def foo(self):
if hasattr(self, 'a'):
print(self.a)
警告,这是一个坏主意!但是,您可以进入 pycharm 首选项 > 编辑器 > 检查,然后在 Python 部分下,禁用未解析的引用"检查.这会让pycharm忽略错误,但这样做是件坏事.
Warning, this is a bad idea! But, you can go into pycharm Preferences > Editor > Inspection, and under the Python section, disable the 'Unresolved references' check. This will make pycharm ignore the error, but it is a bad thing to do.
这篇关于PyCharm 警告 dict 生成的属性未解析的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!