python--some descriptions of the parent class and the child class

Source: Internet
Author: User

The initialization method of a class in Python is __init__ (), so the initialization method of the parent class and subclass is this, and if the subclass does not implement __init__ (), the initialization function of the parent class is called,

If the subclass implements this function, the __init__ () of the parent class is called explicitly in this function, which is not the same as C++,java, which automatically calls the parent class constructor.

# example  of calling a parent class initialization method in initialization # B is a subclass  of a class B (A):         def __init__ (self):            super (). __init__ ()  


Three ways to call other member functions of the parent class:
1. Direct write class name call;

2. Call with super (type, obj) method (ARG).

3. Within the definition of a subclass, if you call a member of the parent class, you can use Super (). Method (Arg) directly.

class A:         def method (self, arg):                return      class B (A):         def method (self, arg):     #         A.method (self,arg)                #1     #        super (B, Self). Method (ARG)        #2      #         Super (). Method (ARG)               #3   

[note] If you are outside the subclass definition (that is, within other function logic, when the subclass object calls the parent class member), follow:

...   = B ()    super (B,ob). Method (ARG)    # calls the method of the parent class A of class B.   ...  

python--some descriptions of the parent class and the child class

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.