The difference between Python parent.__init () and super (Child, self)

Source: Internet
Author: User

What the Super function does

def Supper (CLS, inst):     = Inst.__class__. MRO ()    return mro[mro.index (CLS) + 1]

Inst generating a list of parent classes

Finds the CLS index from the list and returns its next index

Assume

MRO = [A,b,c]

CLS = b

Then the CLS index is 1

Returns the next index, which is mro[2], c

MRO process is method Resolution Order

The Inst method returns all the parent classes of the

Order is

Suppose A is a base class, b,c are subclasses of a, D inherits B and C (inheritance order is b,c)

So the MRO of D is returning

[D,b,c,a]

The use of the Super (D,self) method for D returns the B

See Python Cookbook for more details

Http://python3-cookbook.readthedocs.io/zh_CN/latest/c08/p07_calling_method_on_parent_class.html

The difference between Python parent.__init () and super (Child, self)

Related Article

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.