Section 8.8 Python __new__ using methods and constructors __init__ complete class instantiation process detailed

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 )

Section 8.8 Python __new__ using methods and constructors __init__ complete class instantiation process detailed

__New__ previous section describes methods and constructors in the __init__ Python class, and by analyzing the relationship between the two examples, this section summarize systematic Python class instantiation process. Specific steps are as follows:
First, the developer through the object constructor expression "class = instance object (parameter list)" start to create a new instance of the task;
two, Python __new__ method looks like
Python is first instantiated class in preparation in looking new__ method, if the class does not correspond directly __new__ method from the parent class look, if look from the direct parent class does not, the direct parent find direct parent, and so on, until found __new__ a method so far (if no custom class, all classes will be __new__ parent class object class method to find).
Method three __new__, Python to pass parameters like
the type found Python __ new__ corresponding method, and a parameter list object class constructor expression __new__ passed to find methods (if the object class the __new__ method, if only to pass a custom class class name parameter or parameters are passed old ape has not been confirmed) as a parameter, and execute the code of the method;

  1. __new__ method in the application and object constructor must trigger expression of at least one parameter CLS, representative of the class to instantiate objects, this parameter is automatically provided by the Python interpreter at instantiation;
  2. Conventionally, when new__ __ method performed, in addition to the method __new__ object class, all classes must be rewritten __new__ method __new__ method call parent class, and when calling the method of the present the first argument passed cls __new__ parent class as the first parameter;
  3. Since __new__ defined class method calls incoming cls, in addition to passing layer by layer, but also can be adjusted, in some cases, may be modified to correspond to other types of arguments required by the application cls parameter;
  4. In the calling hierarchy, __new__ method object class must be one of the last call;
  5. Each class definition from __new __ () method can not call itself __new __ in the method () to create instance, you must call the parent class method, otherwise it will cause an infinite loop.
    Four, the method returns an instance of an object __new__
    __new__ Object class returns an instance of a first parameter corresponding to the calling cls custom subclass thereof, custom subclass __new__ method returns to its caller continues according to the situation of the subclasses __new__ method until you return to the object constructor expression of the caller.
    All parent must return after performing the method __new__ a subclass instance object, in this instance of the object is conventionally __new__ method cls parameter corresponding to the first instance of a "class", then the instance of the subclass is returned to its the caller, passed back to the caller until the beginning or "object constructor expression" corresponding application;
  6. Since the self-defined class method __new__ incoming call cls can be modified, since the upper __new__ method defined classes may also vary the value and type of the return value, it is not necessarily possible to return an instance of the object constructor expression " type "corresponding to" category ";
  7. Use __new __ () method of changing the instance type is primarily to allow return of immutable type (e.g., int, str, tuple) custom subclass instances, there is achieved a custom metaclass. About metaclass (metaclass) content not old ape-depth study, it will not introduce.

Five, Python determines whether __new__ method returns the object instance is the object constructor expression "class" of cls instance, if it is not does not call any class constructor (even if the parent class nor instance), directly examples returned to the caller object constructor expression, otherwise go to the next step;

  1. In this regard, the old ape verify several cases will not perform any type of construction method, but in the case of some of the information accessible online, there is that can be performed to return the corresponding class constructor instance, the old ape never encountered this case, but maybe there really such a case, you may need certain conditions to trigger, so this doubt in this record.
    Six, __new__ after completion of execution method, if the returned instance is an instance of class cls, as the example of this self passed to the constructor, and the process parameters except __new__ cls also passed to the constructor method;
    VII. the constructor is executed, executed after the constructor returns to the instance "instance object" left-hand expression in the object constructor, the completion of the entire class instantiation process.

Old ape Python series of articles for the summary of the old ape gradually introduce learning Python learning experience, which helps no contact with Python programmers can easily enter the Python world.
Welcome criticism, thank you attention!

Guess you like

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