How the Python neutron class invokes the parent class method

Source: Internet
Author: User

The initialization method for classes in Python is __init__ (), therefore, the initialization method of the parent class subclass is this, if the subclass does not implement this function, the initialization function of the parent class is invoked when the subclass implements this function, and the parent class is overwritten with this function, now that the parent class is inherited, It is necessary to explicitly call the __init__ () of the parent class in this function, unlike C++,java, who automatically invokes the parent class initialization function.

Calling the parent class function has the following methods:

Class A:

Def method (self, arg):

Pass

Class B (A):

Def method (self, arg):

# A.method (self,arg) # 1

# super (B, Self). Method (ARG) #2

Super (). Method (ARG) #3

1. Direct Write class name call

2. Called with super (type, obj). Method (ARG).

3. Call the parent class method of this class in the class definition directly to Super (). Methods (ARG).

Example of a method that calls the parent class of an object in code:

OB = B ()

Super (B,ob). Method (Arg) #调用class the method of parent Class A of B.

Example of invoking the parent class initialization method in initialization:

Class B (A):

def __init__ (self):

Super (). __INIT__ ()

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.