Inherit from the face of the object | Python

Source: Internet
Author: User

1.  Son inherits the base class of parent, and can call the methods in the base class directly, but if you want to add a new property under the __init__ () method in the subclass, you need to call the __init__ () method of the base class actively, because the subclass does not automatically call the base class's __init__ () method. classSon (Parent):def __init__(self,name): Parent.__init__(self) self.name='Mic'    defRun (self):Pass2. On top of the base class method, add some more actions:classSon (Parent):defRun (self):Print('---Add---') Parent.run (self)#Note: the instance created by the current class calls the run () method and is not implemented in the parent class;  #--->> introducing super ()  classSun (Parent):defRun (self):Print('---Add---') Super (son,self). Run ()#The advantage of super () is that you do not need to specify a base class inheritance method, from all base classes    #find this method to inherit;  #-->> simplifies the super in Python3:super (). Run ()3. Attribute lookup order in multiple inheritance#two lines are available: location, specificity:  #position, which indicates the position of the parameter at the time of inheritance;  #particularity, indicating whether there is a parent class, and there are several layers of parent class;    #In fact, the order of the base class lookup is determined by the C3 linearization algorithm, rather than the Internet .  #depth first, breadth first such a simple algorithm;  #In the process of inheriting the design of a class, there may be problems with validation, and the location and particularity conflict:      classX (object):Passclas Y (X):Pass      classZ (x, y):Pass  #The above example will be an error; TypeError

Inherit from the face of the object | Python

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.