Detailed description of python class inheritance instances and python instance details

Source: Internet
Author: User

Detailed description of python class inheritance instances and python instance details

Python class inheritance

For many articles that explain the inheritance of python classes, most of them refer to some concepts such as oop and polymorphism. I think this may not help developers who have a certain degree of foundation, it is better to directly use the code written in various situations to demonstrate the effect of code running on a specific code scenario. This may help developers more. Put the Code directly without talking nonsense.

Classic classes and new classes are not distinguished here. The analysis below applies to both new classes and classic classes.

For the _ init _ function in the class, it is only a function called during initialization (ps: initialization and instance creation are not a process, the instance is created using a create FUNCTION). If the Declaration _ init _ function is not displayed in the subclass, the subclass calls the _ init _ function of the parent class, but does not call the _ init _ function of the parent class, if the _ init _ function is declared, the initialization function of the parent class will not be called during subclass initialization, only the _ init _ FUNCTION declared in the subclass is called, and the attributes declared in the _ init _ function of the parent class are not found in the subclass 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 running result is

{}

When the dog class is changed to the following (ps: the _ init _ method is not displayed at this time ):

class dog(animal): def run(self):  print "dog is running..."

In this case, because the _ init _ method of the parent class animal is called directly, the result is as follows:

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

What happens if a class inherits the dog class? (The python interpreter first looks for the _ init _ method in the subclass. If not, find it from the parent class until it is found and run the command, in addition, the _ init _ method of the parent class is no longer running. The created attribute in the _ init _ method that is not running does not exist. For example, in the preceding example, the weight and height attributes are not found in the example obtained during the first running)

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 __init__(self): #  self.ji="jinmao" passif __name__=="__main__": dg=jinmao() print dg.__dict__

The result is the same

{}

What happens to the multi-inheritance of classes? (In multi-inheritance, the _ init _ function in the parent class declared by the first parameter in the inheritance parameter of the class will be run. If the parent class does not have the _ init _ function, continue to find the parent class of the parent class, and so on... If no header is found, replace it with the parent class declared by the second parameter. For example, if the first parameter is found, the _ init _ function is found. If none is found, the code can be tested based on the previous example.

The usage and features of super that calls methods in the parent class in subclass.

Summary:If you want to know how to create a class for a python Virtual Machine (specifically when a class is loaded. in the pyc file, how does one put class objects in a piece of memory through the C language code logic? I am also learning about this and hope we can explore it together)

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

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.