super()和Parent类名之间有什么区别? [英] What's the difference between super() and Parent class name?

查看:217
本文介绍了super()和Parent类名之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 super()和直接使用父类名称之间有区别吗?例如:

Is there a difference between using super() and using the parent class name directly? For example:

class Parent:
    def __init__(self):
        print("In parent")
        self.__a=10

class Child(Parent):
    def __init__(self):
        super().__init__()     # using super()
        Parent.__init__(self)  # using Parent class name

c=Child()

super().__ init __()之间内部有区别。父.__ init __(自我)

推荐答案

不在这种情况下。但一般,尤其是当您使用多重继承时, super()委托给下一个对象在方法解析顺序(MRO) =nofollow noreferrer>文档

Not in this case. But in general, and especially when you use multiple inheritance, super() delegates to the next object in the Method Resolution Order (MRO) as is specified in the documentation:


super([type [,object] -or-type]])

super([type[, object-or-type]])

返回一个代理对象,将方法调用委托给a parent或
兄弟类的类型。这对于访问已在类中重写的继承方法
非常有用。搜索顺序与 getattr()使用的
相同,只是跳过类型本身。

Return a proxy object that delegates method calls to a parent or sibling class of type. This is useful for accessing inherited methods that have been overridden in a class. The search order is same as that used by getattr() except that the type itself is skipped.

该类型的 __ mro __ 属性列出了 getattr() $ c>和 super()。属性
是动态的,只要继承层次结构是
更新,就可以更改。

The __mro__ attribute of the type lists the method resolution search order used by both getattr() and super(). The attribute is dynamic and can change whenever the inheritance hierarchy is updated.

(...)

(复制,粗体添加)

比如说你定义了类(借用这个问题,更详细地讨论了MRO

Say for instance you define classes like (borrowed from this question, where the MRO is discussed in more detail):

class F:
    def __init__(self):
        print('F%s'%super().__init__)
        super().__init__()

class G: 
    def __init__(self):
        print('G%s'%super().__init__)
        super().__init__() 

class H: 
    def __init__(self):
        print('H%s'%super().__init__)
        super().__init__()

class E(G,H):
    def __init__(self):
        print('E%s'%super().__init__)
        super().__init__()

class D(E,F): 
    def __init__(self):
        print('D%s'%super().__init__)
        super().__init__() 

class C(E,G): 
    def __init__(self):
        print('C%s'%super().__init__)
        super().__init__()

class B(C,H): 
    def __init__(self):
        print('B%s'%super().__init__)
        super().__init__()

class A(D,B,E): 
    def __init__(self):
        print('A%s'%super().__init__)
        super().__init__()

然后 __ mro __ A 是:

A.__mro__ == (A,D,B,C,E,G,H,F,object)

现在,如果我们调用 A(),它会打印:

Now if we call A(), it prints:

A<bound method D.__init__ of <__main__.A object at 0x7efefd8645c0>>
D<bound method B.__init__ of <__main__.A object at 0x7efefd8645c0>>
B<bound method C.__init__ of <__main__.A object at 0x7efefd8645c0>>
C<bound method E.__init__ of <__main__.A object at 0x7efefd8645c0>>
E<bound method G.__init__ of <__main__.A object at 0x7efefd8645c0>>
G<bound method H.__init__ of <__main__.A object at 0x7efefd8645c0>>
H<bound method F.__init__ of <__main__.A object at 0x7efefd8645c0>>
F<method-wrapper '__init__' of A object at 0x7efefd8645c0>
<__main__.A object at 0x7efefd8645c0>

所以这意味着 A 并在尝试获取 __ init __ 时:

so it means that in the context of A and when trying to obtain __init__ that:


  • super().__ init __ of A D. __init __ ;

  • super().__ init __ of D B .__ init __ ;

  • super().__ init__ B C .__ init __ ;

  • super().__ init __ C E .__ init __ ;

  • super().__ init __ of E is G .__ init __ ;

  • super().__ init __ of G H .__ init __ ;

  • super().__ init __ H F .__ init __ ;和

  • super().__ init __ F object .__ init __

  • super().__init__ of A is D.__init__;
  • super().__init__ of D is B.__init__;
  • super().__init__ of B is C.__init__;
  • super().__init__ of C is E.__init__;
  • super().__init__ of E is G.__init__;
  • super().__init__ of G is H.__init__;
  • super().__init__ of H is F.__init__; and
  • super().__init__ of F is object.__init__.

请注意 super() 本身不代表父母。例如 super() D B B 不是 D 的超类,所以它真的取决于对象的类型(不在课堂上)。

Note thus that super() does not per se delegates to a parent. For instance the super() of D is B and B is not a superclass of D, so it really depends on the type of the object (not on the class).

现在,如果 D __ mro__ 是:

D.__mro__ = (D,E,G,H,F,object)

如果我们构建 D 但我们得到:

If we construct a D however we get:

D<bound method E.__init__ of <__main__.D object at 0x7efefd864630>>
E<bound method G.__init__ of <__main__.D object at 0x7efefd864630>>
G<bound method H.__init__ of <__main__.D object at 0x7efefd864630>>
H<bound method F.__init__ of <__main__.D object at 0x7efefd864630>>
F<method-wrapper '__init__' of D object at 0x7efefd864630>

所以 D 它认为:

So in the context of D it holds that:


  • super().__ init __ D E .__ init __ ;

  • super().__ init __ E G .__ init __ ;

  • super().__ init __ G H .__ init __ ;

  • super().__ init __ of H F .__ init __ ;和

  • super().__ init __ F object .__ init __

  • super().__init__ of D is E.__init__;
  • super().__init__ of E is G.__init__;
  • super().__init__ of G is H.__init__;
  • super().__init__ of H is F.__init__; and
  • super().__init__ of F is object.__init__.

所以这里 super( ) D 导致 E (对于 __ init __ A 的上下文中不一样。

So here the super() of D leads to E (for __init__) which is not the same in the context of A.

这篇关于super()和Parent类名之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆