Python class inheritance Explained

Source: Internet
Author: User
For many articles about the inheritance of Python class, most of what is said about OOP, polymorphism and other concepts, I think this may be a certain basis of the developer help is not so large, rather than directly with the code written in various situations, to show the code for a certain situation, the operation will have what effect. This may be more helpful to developers. Do not say nonsense, directly on the code.

There is no distinction between classic and modern classes, and the following analysis applies to both modern and classic classes.

For the init function in a class, just one initialization is called a function (PS: initializing and creating an instance is not a procedure, the creation of an instance is done by a CREATE function), and if the declaration of the INIT function is not shown in the subclass, the subclass invokes the Init function of the parent class. However, the Init function in the parent class is no longer called, and if the INIT function is declared, the initialization function of the parent class will not be called when the subclass is initialized, only the INIT function declared in the subclass will be called, and there will be no attributes declared in the parent class init function in the child class instance:

Class Animal ():    name= "hh"    sex= "man"    def init (self):        self.height=10        self.weight=50    def Deception (self):        print "ansible.height:" +self.height+ "Animal.weight:" +self.weight    def Run (self):        Print "Animal is running ...." Class Dog (Animal):    def init (self):        passif name== "main":   Dg=dog ()   Print Dg.dict

The result of this run is

{}

When changing the dog class to the following (PS: Declaring the Init method not shown at this time):

Class Dog (animal):    def run:        print "Dog is running ..."

At this point, the Init method of the parent class animal is called directly, the result is as follows:

{' weight ': +, ' height ': 10}

What happens if a class inherits the dog class at this point? (The Python interpreter looks for the Init method in the subclass first, if it is not found, then from the parent class, until it is found, and runs, and no longer runs the Init method of the parent class's parent class, at which point the Created property in the Init method is not running, such as the example above, The first run-time example does not have the weight and height properties)

Class Animal ():    name= "hh"    sex= "    def init (self):        self.height=10        self.weight=50    def Deception (self):        print "ansible.height:" +self.height+ "Animal.weight:" +self.weight    def Run (self):        Print "Animal is running ...." Class Dog (Animal):    def init:        pass    def run (self):        print "Dog is" Running ... "Class Jinmao (dog):    # def init (self):    #     self.ji=" Jinmao "    passif name==" main ":   Dg=jinmao ()   print dg.dict

At this point the result is the same

{}

What happens in multiple inheritance of a class? (in multi-inheritance, the first parameter in the class's inheritance parameter declares the INIT function in the parent class, if there is no init function in the parent class, continue to look for the parent class in the parent class, and so on ...) If a header is found or not, the second argument is declared as the parent class, and the procedure, such as the first parameter, knows that the init function is found, and if none is found, then the code can test itself according to the previous example.

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.