【发布时间】:2017-07-16 07:08:09
【问题描述】:
我从 PyCharm 收到有关未解析引用的警告。 这是结构类似的代码,也应该收到警告。
class Parent:
"""
=== Attributes ===
@type public_attribute_1: str
"""
def __init__(self):
public_attribute_1 = ''
pass
class Child(Parent):
"""
=== Attributes ===
@type public_attribute_1: str
> Unresolved reference 'public_attribute_1'
@type public_attribute_2: str
"""
def __init__(self):
Parent.__init__(self)
public_attribute_2 = ''
pass
我知道public_attribute_1 不是在Child.__init__() 中显式启动的,而是Child.__init__() 调用Parent.__init__(self) 启动public_attribute_1。因此,引发的错误与功能性和可读性有关。
如何在不插入冗余的情况下使此类代码更具可读性,从而破坏整个继承点? 通过 docstrings 和 cmets 彻底记录并忽略 PyCharm 的警告就足够了吗?还是有pythonic的方法?
【问题讨论】:
-
浮动的
passes 是怎么回事?你也应该使用super().__init__()。 -
我使用了
pass,因为这些类中的其余代码并不重要。我想他们不是真的有必要,但是是的。
标签: python-3.x inheritance documentation pycharm