Https://docs.python.org/2/tutorial/classes.html#multiple-inheritance
http://www.jackyshen.com/2015/08/19/multi-inheritance-with-super-in-Python/
http://python.jobbole.com/85685/
In multiple inheritance, how to access a property in the parent class is determined in the order in __mro__ .
About Super (), the prototype is this:
super
(type[, object-or-type])
Note:if The second argument is omitted, and the Super object returned is unbound. If The second argument is a object, isinstance(obj, type)
must be true. If The second argument is a type, issubclass(type2, type)
must are true (this is useful for classmethods).
At initialization time, if Super () (Ref:https://docs.python.org/2/library/functions.html#super) is used,
Then the first parent class encountered is initialized in the order in __mro__
If you want to initialize all of the parent classes, you need to use explicit methods, such as defining classes:
A, B, C (A, B)
Then in C you can initialize the parent class A:
A.__init__ (self)
If the constructor of a is not called, then the variable defined in the constructor of a is inaccessible, although C is a subclass of
Because the variable defined in the constructor of a is not bound to an instance of C
But the method of a can still be called, and the order of invocation is determined by __mro__:
classA (object):def __init__(self): SELF.A= 1Print 'A' defSing (self):Print 'Sing A'classB (object):def __init__(self):Print 'B'classC (A, B):def __init__(self):Print 'C'C=C () c.sing () # OKPrintC.A # Error
The full name of the MRO is method resolution order, which defines the order in which the parent class is accessed in the case of multiple inheritance
The MRO is described in the document (Https://docs.python.org/2/library/functions.html#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 whenever, the inheritance hierarchy is updated.
At present, the new class in Python2.7, the class acquisition MRO in Python3 uses the C3 algorithm (ref:https://en.wikipedia.org/wiki/c3_linearization)
Some issues in Python multiple inheritance