简体   繁体   English

为什么python中的继承要求父类显式继承对象?

[英]Why inheritance in python require the parent class to inherit object explicitly?

Below is two versions of my code: 以下是我的代码的两个版本:

Non-working one 不工作的人

class A:
    def __init__(self):
        print "I am A "

class B:
    def __init__(self):
        print "I am B "

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

c = C()

This raises exception: 这引发了异常:

    super(C, self).__init__()  
TypeError: must be type, not classobj  

Working version 工作版本

class A(object):
    def __init__(self):
         print "I am A "

class B:
    def __init__(self):
         print "I am B "

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

c = C()

If one of the parent class has inherited object explicitly , there is no exception and things works as desired. 如果父类之一显式继承了object ,则没有异常,并且一切正常。 Any explanation, why? 任何解释,为什么?

With the above working one , it prints "I am A" but not "I am B" which means it initializes only Class A and not Class B. HOw to initialize multiple parent classes in children class? 通过上述工作 ,它将显示“我是A”而不显示“我是B”,这意味着它仅初始化A类,而不初始化B。如何在子类中初始化多个父类?

That's because super only works with new-style classes (that inherit from object ). 这是因为super仅适用于新型类(继承自object )。 In Python 2, classes where object is nowhere in the inheritance hierarchy (like your first example) are called old-style classes, and should never be used anywhere. 在Python 2中, object在继承层次结构中不存在的类(如您的第一个示例)被称为类,并且永远不要在任何地方使用。

It's an old historical artifact from when Python first got OO added to it, and the developers got it wrong. 这是从Python首次添加OO到开发人员弄错时的一个古老历史工件。 Among other things, new-style classes allow for the use of super , the descriptor protocol (properties), and makes several fixes to the way multiple inheritance is handled. 除其他外,新型类允许使用super ,描述符协议(属性),并对处理多重继承的方式进行了一些修复。 super is one of them, but the most important is the way method resolution order is computed for diamond inheritance cases (A inherits from B and C which both inherit from D). super是其中之一,但最重要的是钻石继承案例(A继承自B和C都继承自D)的方法解析顺序的计算方式。 Read more about it here: http://python-history.blogspot.fr/2010/06/method-resolution-order.html 在此处了解更多信息: http : //python-history.blogspot.fr/2010/06/method-resolution-order.html

Note that Python 3 ditched old-style classes entirely, and thus in Python 3 all classes are new-style classes and inherit from object, regardless of whether or not they do it explicitly. 请注意,Python 3完全放弃了旧式类,因此在Python 3中,所有类都是新式类,并且从对象继承,而不管它们是否显式执行。

super() only works for new-style classes(Any class which inherits from object).So You couldn't pass class object to super . super()仅适用于新型类(任何从object继承的类)。因此您无法将class object传递给super

There are two typical use cases for super. 超级有两种典型的用例。 In a class hierarchy with single inheritance, super can be used to refer to parent classes without naming them explicitly, thus making the code more maintainable. 在具有单一继承的类层次结构中,super可以用于引用父类而无需显式命名它们,从而使代码更具可维护性。 This use closely parallels the use of super in other programming languages. 这种用法与其他编程语言中super的用法非常相似。

The second use case is to support cooperative multiple inheritance in a dynamic execution environment. 第二个用例是在动态执行环境中支持协作式多重继承。 This use case is unique to Python and is not found in statically compiled languages or languages that only support single inheritance. 该用例是Python独有的,在静态编译语言或仅支持单继承的语言中找不到。 This makes it possible to implement “diamond diagrams” where multiple base classes implement the same method. 这使得在多个基类实现相同方法的情况下实现“菱形图”成为可能。 Good design dictates that this method have the same calling signature in every case (because the order of calls is determined at runtime, because that order adapts to changes in the class hierarchy, and because that order can include sibling classes that are unknown prior to runtime). 良好的设计要求该方法在每种情况下都具有相同的调用签名(因为调用的顺序是在运行时确定的,因为该顺序适合于类层次结构中的更改,并且因为该顺序可以包含在运行时之前未知的同级类)。

a typical superclass call looks like this: 典型的超类调用如下所示:

class C(B):
    def method(self, arg):
        super(C, self).method(arg)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM