Some problems in Python multiple inheritance

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.