Examples of inheritance of classes in Python

Source: Internet
Author: User
This article mainly introduces the Python class inheritance instance details of the relevant information, the need for friends can refer to the following

Inheritance of Python classes

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 __init__ function is not displayed in the subclass, the subclass calls the parent class's _ The _init__ function, but no longer calls the __INIT__ function in the parent class, if the __INIT__ function is declared, the initialization function of the parent class is not called when the subclass is initialized, and only the __INIT__ function declared in the subclass is called. There is also no attribute declared in the parent class __init__ function in the child class instance.

Example:

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: Declaration __init__ method not shown at this time):

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

At this point, because 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 first looks for the __init__ method in the subclass, if it is not found, then finds it 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 property created in the __init__ method is not running, For example, in the example above, there are no weight and height attributes for the first run.

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__ (self):  pass def Run (self):  print "Dog is running ..." Class Jinmao (dog): # def __ini T__ (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 multiple inheritance, the __INIT__ function in the parent class of the first parameter of the class's inheritance argument is run, 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 is the first argument, until the __init__ function is found, if none is found, then the code can be tested on its own, according to the previous example.

The use and attributes of super for calling a method in a parent class in a subclass.

Summary: If you want to know how a Python virtual machine is created (specifically, when loading a. pyc file, how to put the class object into a piece of memory by the C code logic, I also understand the content, I hope we explore)

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.