Section 8.5 Python class methods and constructors __init__ __new__-depth analysis of the relationship: the relationship between the order of execution and parameters Detailed Case

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

On section describes __new __ () method that is more important than the constructor method syntax, this section analyzes in detail through case __new __ (relationship between the details of the method and its structure and methods).
First, the case described
in this section Cir Case of a circle described, to illustrate the problem of class defines only the basic information contains only one member of the radius RADIUS, two methods, that is __new __ () methods and constructors __init __ () method .
For easy viewing information, we will return the relevant parameters and information output value.
Second, the case of the code
1. class definition

class Cir():
   def __new__(cls,*args,**kwargs):
        #看看Python传递给__new__的参数
       print("Python传递给__new__的参数:\n***cls: ",cls,"\n***args: ",args,"\n***kwargs:",kwargs)
       inst = super().__new__(cls)
       print("__new__返回值:",inst)
       return inst
        
   def __init__(self,radius): 
       print("In init,self的值为:",self,", radius的值为:",radius)
           self.radius = radius


2. Create an instance of the class and View (interactive mode execution)

c=Cir(10)
c #查看c的数据
Cir #查看类的数据

3. Case screenshots and interpretation


 
As can be seen from the above screenshot following three points:
. 1) before the method is performed __new__ __init__ method;
2) from where it can be seen in the above figure labeled red pen, __ call the parent class method returns new__ in __new__ examples of self inst value and the init, c and instances created after the value at the address is the same, show a three, and is returned by the method of the parent class __new__;
3) from the above figure yellow mark portion, can be seen the method of the parameters is cls __new__ Cir corresponding class.

This section describes the case by __new __ () method and the relationship between the portion __init__ method, summarized as follows:
1. __new__ is performed before the method __init__ method, after __ new__ method returns an instance of an object execution, i.e. ; said __new__ method before executing instance did not create, parameter self constructor is passed after the last instance of execution __new__ method
parameter 2. __new__ method is automatically passed by the Python, the parameters include creating instances the "class name" corresponding to the class, and create instances create all the parameters passed.
Old ape Python (https://blog.csdn.net/LaoYuanPython) series of articles for the gradual introduction summary of the old ape learning Python learning experience, which helps no contact with Python programmers can easily enter Python world.
Welcome criticism, thank you attention!

Guess you like

Origin blog.csdn.net/LaoYuanPython/article/details/93639520