Python 3.x Docstring-Python 3.6-Pycharm 17.1.4中的未解析引用警告
我收到PyCharm关于未解析引用的警告。 这里的代码结构类似,并且应该收到警告Python 3.x Docstring-Python 3.6-Pycharm 17.1.4中的未解析引用警告,python-3.x,inheritance,documentation,pycharm,Python 3.x,Inheritance,Documentation,Pycharm,我收到PyCharm关于未解析引用的警告。 这里的代码结构类似,并且应该收到警告 class Parent: """ === Attributes === @type public_attribute_1: str """ def __init__(self): public_attribute_1 = '' pass class Child(Parent): """ === Attributes ===
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\u属性\u 1
不是在Child.\uuu init\uuu()
中显式启动的,而是Child.\uu init\uuu()
调用父项。\uu init\uuu(self)
启动public\u属性\u 1
。因此,出现的错误更多地涉及可读性和文档,而不是功能
如何使这样的代码更具可读性,而不插入冗余,从而破坏整个继承点?
通过docstring和注释进行完整的文档记录,而忽略PyCharm的警告就足够了吗?还是有一种类似于蟒蛇的方法呢?这里有很多问题
python3
中使用super()
self
掩盖细节Child
的docstring
的Parent
的所有属性有关。从可维护性的角度来看,这是非常危险的,但如果有任何变化的话。当我看到一个类继承了父类时,如果我不熟悉父类,我将转到父类的定义(这使得使用Ctrl+B
很容易)
我会让其他人说这是否真的是pythonic,但这是我习惯的工作方式。无论如何,要修复继承,您的代码应该如下所示:
class Parent:
"""
=== Attributes ===
@type public_attribute_1: str
"""
def __init__(self):
self.public_attribute_1 = ''
class Child(Parent):
"""
=== Attributes ===
@type public_attribute_2: str
"""
def __init__(self):
super().__init__()
self.public_attribute_2 = ''
print(self.public_attribute_1)
浮动
pass
es有什么用?您还应该使用super()。\uuu init\uuu()
。我使用了pass
,因为这些类中的其余代码并不重要。我想他们不是真的需要,但是是的。谢谢你的投入!我可以问一下Parent.\uuu init\uuu(self)
和super.\uu init\uuu()
之间的区别吗?我通过一门课程学习python,通过使用Parent.\uuuu init\uuuuu(self)
学习继承,这门课程是针对python 3的。