我到处都看到应该通过以下方式调用超类方法:
super(SuperClass, instance).method(args)
Run Code Online (Sandbox Code Playgroud)
这样做有什么不利之处:
SuperClass.method(instance, args)
Run Code Online (Sandbox Code Playgroud) 在Python 2.7和3中,我使用以下方法来调用超类的函数:
class C(B):
def __init__(self):
B.__init__(self)
Run Code Online (Sandbox Code Playgroud)
我看到也可以B.__init__(self)
用super(B, self).__init__()
和替换python3 super().__init__()
.
这样做有什么优点或缺点吗?B
至少直接为我调用它更有意义,但也许有一个很好的理由super()
只能在使用元类时使用(我通常会避免).