Bit*_*rex 7 python constructor class
在学习Python时,我对使用继承的类初始化语法感到困惑.在各种例子中,我看到过如下内容:
class Foo(Bar):
def __init__(self, arg, parent = None):
Bar.__init__(self, parent)
self.Baz = arg
etc.
Run Code Online (Sandbox Code Playgroud)
虽然有时它只是
class Foo(Bar):
def __init__(self, arg):
Bar.__init__(self)
etc.
Run Code Online (Sandbox Code Playgroud)
什么时候想确保使用"parent"作为初始化函数的参数?谢谢.
通常parent
,只有当父类的构造函数明确需要这样的参数时,才能传递所需的内容.这在某些层次结构中使用,例如PyQt.
父类初始化的一个好习惯就是使用super
:
class Child(Father):
def __init__(self):
super(Child, self).__init__()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8778 次 |
最近记录: |