This article mainly introduces detailed information about the inheritance instances of the python class. For more information about the inheritance instances of the python class, see the following article, for more information, see
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)
The above is the details about class inheritance in python. For more information, see other related articles in the first PHP community!