有什么区别:

class Child(SomeBaseClass):
    def __init__(self):
        super(Child, self).__init__()

and:

class Child(SomeBaseClass):
    def __init__(self):
        SomeBaseClass.__init__(self)

我看到super在只有单一继承的类中被大量使用。我可以理解为什么在多重继承中使用它,但不清楚在这种情况下使用它的优势是什么。


当前回答

这里有一些很好的答案,但它们没有解决在层次结构中的不同类具有不同签名的情况下如何使用super()……特别是在__init__的情况下

为了回答这个问题并能够有效地使用super(),我建议阅读我的答案super()并更改合作方法的签名。

下面是这种情况的解决方案:

层次结构中的顶级类必须继承自自定义类,如SuperObject: 如果类可以接受不同的参数,始终将接收到的所有参数作为关键字参数传递给超函数,并且始终接受**kwargs。

class SuperObject:        
    def __init__(self, **kwargs):
        print('SuperObject')
        mro = type(self).__mro__
        assert mro[-1] is object
        if mro[-2] is not SuperObject:
            raise TypeError(
                'all top-level classes in this hierarchy must inherit from SuperObject',
                'the last class in the MRO should be SuperObject',
                f'mro={[cls.__name__ for cls in mro]}'
            )

        # super().__init__ is guaranteed to be object.__init__        
        init = super().__init__
        init()

使用示例:

class A(SuperObject):
    def __init__(self, **kwargs):
        print("A")
        super(A, self).__init__(**kwargs)

class B(SuperObject):
    def __init__(self, **kwargs):
        print("B")
        super(B, self).__init__(**kwargs)

class C(A):
    def __init__(self, age, **kwargs):
        print("C",f"age={age}")
        super(C, self).__init__(age=age, **kwargs)

class D(B):
    def __init__(self, name, **kwargs):
        print("D", f"name={name}")
        super(D, self).__init__(name=name, **kwargs)

class E(C,D):
    def __init__(self, name, age, *args, **kwargs):
        print( "E", f"name={name}", f"age={age}")
        super(E, self).__init__(name=name, age=age, *args, **kwargs)

E(name='python', age=28)

输出:

E name=python age=28
C age=28
A
D name=python
B
SuperObject

其他回答

super()在单继承中的好处是微乎其微的——大多数情况下,您不必将基类的名称硬编码到每个使用其父方法的方法中。

然而,如果没有super(),几乎不可能使用多重继承。这包括常见的习惯用法,如mixin、接口、抽象类等。这将扩展到稍后扩展您的代码。如果有人后来想要编写一个扩展Child和mixin的类,他们的代码将无法正常工作。

考虑下面的代码:

class X():
    def __init__(self):
        print("X")

class Y(X):
    def __init__(self):
        # X.__init__(self)
        super(Y, self).__init__()
        print("Y")

class P(X):
    def __init__(self):
        super(P, self).__init__()
        print("P")

class Q(Y, P):
    def __init__(self):
        super(Q, self).__init__()
        print("Q")

Q()

如果将Y的构造函数更改为X.__init__,你将得到:

X
Y
Q

但是使用super(Y, self).__init__(),你会得到:

X
P
Y
Q

P或Q甚至可能涉及到另一个文件,当你写X和Y时,你不知道,所以基本上,当你写类Y(X)时,你不知道super(Child, self)会引用什么,甚至Y的签名也像Y(X)一样简单。这就是为什么超级可能是更好的选择。

所有这些不都假定基类是一个新型的类吗?

class A:
    def __init__(self):
        print("A.__init__()")

class B(A):
    def __init__(self):
        print("B.__init__()")
        super(B, self).__init__()

将不能在python2中工作。类A必须是new-style,即:类A(对象)

class Child(SomeBaseClass):
    def __init__(self):
        SomeBaseClass.__init__(self)

这很容易理解。

class Child(SomeBaseClass):
    def __init__(self):
        super(Child, self).__init__()

如果你使用super(Child,self)会发生什么?

当创建子实例时,它的MRO(方法解析顺序)是基于继承的(Child, SomeBaseClass, object)顺序。(假设SomeBaseClass除了默认对象之外没有其他父对象)

通过在self实例的MRO中传递Child、self、super搜索,并返回Child的next代理对象,在本例中是SomeBaseClass,该对象随后调用SomeBaseClass的__init__方法。换句话说,如果它是super(SomeBaseClass,self) super返回的代理对象将是object

对于多继承,MRO可以包含许多类,所以基本上super可以让您决定从MRO中的哪个位置开始搜索。

这里有一些很好的答案,但它们没有解决在层次结构中的不同类具有不同签名的情况下如何使用super()……特别是在__init__的情况下

为了回答这个问题并能够有效地使用super(),我建议阅读我的答案super()并更改合作方法的签名。

下面是这种情况的解决方案:

层次结构中的顶级类必须继承自自定义类,如SuperObject: 如果类可以接受不同的参数,始终将接收到的所有参数作为关键字参数传递给超函数,并且始终接受**kwargs。

class SuperObject:        
    def __init__(self, **kwargs):
        print('SuperObject')
        mro = type(self).__mro__
        assert mro[-1] is object
        if mro[-2] is not SuperObject:
            raise TypeError(
                'all top-level classes in this hierarchy must inherit from SuperObject',
                'the last class in the MRO should be SuperObject',
                f'mro={[cls.__name__ for cls in mro]}'
            )

        # super().__init__ is guaranteed to be object.__init__        
        init = super().__init__
        init()

使用示例:

class A(SuperObject):
    def __init__(self, **kwargs):
        print("A")
        super(A, self).__init__(**kwargs)

class B(SuperObject):
    def __init__(self, **kwargs):
        print("B")
        super(B, self).__init__(**kwargs)

class C(A):
    def __init__(self, age, **kwargs):
        print("C",f"age={age}")
        super(C, self).__init__(age=age, **kwargs)

class D(B):
    def __init__(self, name, **kwargs):
        print("D", f"name={name}")
        super(D, self).__init__(name=name, **kwargs)

class E(C,D):
    def __init__(self, name, age, *args, **kwargs):
        print( "E", f"name={name}", f"age={age}")
        super(E, self).__init__(name=name, age=age, *args, **kwargs)

E(name='python', age=28)

输出:

E name=python age=28
C age=28
A
D name=python
B
SuperObject