Python is similar to other object-oriented languages, and each class can have one or more parent classes that inherit properties and methods from the parent class. If a method is invoked in an instance of a subclass, or if a property is accessed in an instance of a subclass, but the method or property does not exist in the subclass, it is automatically searched in its parent class.
After inheriting the parent class, you can invoke the parent class method and access the parent class property, and to complete the integration process, the subclass is the constructor that needs to be invoked.
A subclass does not explicitly call the constructor of the parent class, and the parent class constructor initializes some properties, and a problem occurs
If both the subclass and the parent class have constructors, subclasses actually rewrite the constructor of the parent class, and if the parent class constructor is not explicitly invoked, the constructor of the parent class is not executed, causing the child class instance to access the problem with the initial variable in the parent class initialization method.
The code is as follows |
Copy Code |
Class A: def __init__ (self): Self.namea= "AAA" def Funca (self): Print "Function A:%s"%self.namea Class B (A): def __init__ (self): self.nameb= "BBB" def FUNCB (self): print ' function B:%s '%self.nameb B=b () Print B.nameb B.FUNCB () B.funca () |
Results:
Bbb
The code is as follows |
Copy Code |
function b:bbb Traceback (most recent call last): File "d:workbenchpythonmypythonprojectteststudyoverwrite_method.py", line, in <module> Print B.funca () File "d:workbenchpythonmypythonprojectteststudyoverwrite_method.py", line 6, in Funca Print "Function A:%s"%self.namea ATTRIBUTEERROR:B instance has no attribute ' Namea ' |
In subclasses, constructors are overridden, but the new constructor does not have any code for initializing the Namea property of the parent class, and in order to achieve the desired effect, the constructor method of the subclass must call its parent class's constructor to perform the basic initialization. There are two ways to do this: call the unbound version of the superclass constructor, or use the Super function.
Method One: Call unbound superclass constructor
to modify the code, add more lines:
code is as follows |
copy code |
Class A: def __init__ (self): self.namea= "AAA" def Funca (self): print "function A:%s"%self.namea Class B (a): def __init__ (self): & nbsp; #这一行解决了问题 a.__init__ ( Self) self.nameb= "BBB" def FUNCB (self): print function B:%s "%self.nameb B=b () Print B.nameb B.FUNCB () B.funca () |
If a line with a comment resolves the problem, call its constructor directly by using the parent class name.
This method is called the unbound constructor method that invokes the parent class. When you call a method of an instance, the self parameter of the method is automatically bound to the instance (called the Binding method). But if you call a method of a class directly (such as A.__init), then no instance is bound. This gives you the freedom to provide the self parameter you want, which is called an unbound unbound method.
By supplying the current instance as the self parameter to an unbound method, Class B can use all implementations of its parent class constructor method, thus namea the variable to be set.
Method Two: Using the Super function
To modify the code, this time you need to add 2 lines to the original code:
The code is as follows |
Copy Code |
#父类需要继承object对象 Class A (object): def __init__ (self): Self.namea= "AAA" def Funca (self): Print "Function A:%s"%self.namea Class B (A): def __init__ (self): #这一行解决问题 Super (B,self). __init__ () self.nameb= "BBB" def FUNCB (self): print ' function B:%s '%self.nameb B=b () Print B.nameb B.FUNCB () B.funca () |
As with the newly added code, the first sentence lets class A inherit from the object class in order to use the Super function because it is a feature of Python's "new class" support. Current mines and objects can be used as parameters of the Super function, and any method that invokes the object returned by the function is the method that invokes the superclass rather than the current class.
The Super function returns a Super object that is responsible for parsing the method, which automatically finds all the parent classes and the parent class of the parent class.
Method One is more intuitive, method two can initialize all superclass at once
The super function is more intuitive than calling unbound methods directly in hyper-fatigue, but the big one is that if a subclass inherits more than one parent class, it only needs to use the Super function once. Without this requirement, however, direct use of a.__init__ (self) is more intuitive.