相关疑难解决方法(0)

Python超级方法和调用替代方案

我到处都看到应该通过以下方式调用超类方法:

super(SuperClass, instance).method(args)
Run Code Online (Sandbox Code Playgroud)

这样做有什么不利之处:

SuperClass.method(instance, args)
Run Code Online (Sandbox Code Playgroud)

python class super

56
推荐指数
2
解决办法
3万
查看次数

super()和直接调用超类之间的区别

在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()只能在使用元类时使用(我通常会避免).

python oop superclass python-2.7 python-3.x

28
推荐指数
1
解决办法
6070
查看次数

标签 统计

python ×2

class ×1

oop ×1

python-2.7 ×1

python-3.x ×1

super ×1

superclass ×1