__new Methods 8.4 Python class constructor was not worth a constructor __ depth analysis: Syntax Interpretation

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 )

I. Introduction
earlier in this blog content to the constructor __init__ were introduced in the previous section also introduces __new__ method, but the old ape think __new__ methods than __init__ constructor should belong to the constructor . This is because in Python, __ new__ method is executed when an object instance is created, and the __init__ constructor is executed after instance creation.

Two, __new__ method described
__new__ new category is introduced in Python, __ new__ commonly used to control the process of generating a new instance. It is a class-level static method, is performed before creating the instance of the object, if the custom class does not override this method, the Python automatically calls the parent class method when all of the parent class does not override the definition of the method, __new__ object class method calls directly. When overridden, syntax related method is as follows:
1. Syntax:
__new __ (CLS, args *, ** kwargs)
wherein:
. 4) corresponding to the class name of the class CLS is created instance, automatically imported from the Python;
. 5) are examples args when you create a non-keyword incoming parameters, packed into a tuple tuple to the body of the function call;
incoming time 6) kwargs create an instance of the keyword parameters in the form dictionary, the key is the keyword keyword arguments, dictionary elements value corresponding to the argument value of the key parameter.

2. Syntax Interpretation:
. 1) __new __ (CLS, args *, ** kwargs) Examples of parameters are defined with the parameters corresponding to the syntax defined in the following examples:
Examples = class name (* args, ** kwargs)
is:
d) __new__ method cls = a generally "class name" corresponding to the class for the instance definition, said to create an instance of the "class name" of the corresponding type, but in some special cases, other classes can be changed If other class into a class, create an instance of what is the type of class. This scenario is particularly useful in some cases, re-introduced later in this section;
E) behind the two parameters args, kwargs Examples of two types of parameters are completely copied definition, shows an example of defining what parameters passed, the method is __new__ add a "class cls" parameters define the parameters on the basis of the example, as the first parameter and cls.
f) args, kwargs than two parameters, are the two parameters, wherein the parameter is the keyword kwargs, args non-keyword parameter by args, kwargs arguments are passed and all conventional keyword arguments passed parameters when creating the instance, it may be zero or more real parameters. * args, ** kwargs this form of parameter passing mode is called "collection parameter" details, refer to this blog "Section 5.2 Python function parameter collected" content;
2) more syntax custom class override _ _new__ method when grammar must be followed when preparing Python automatically calls this method to create an instance, if the custom class does not override this method, then the default Python __new direct parent calls the class __ () method to construct the class examples. If the class does not override the parent class __new __ (), then the rule will always be traced back to __new object Click the __ () method.

__New__ Python method is very magical, is the most critical method when the class is instantiated, __init__ is more important than the construction method, this section describes the syntax and meaning of each parameter __new__, in fact only introduces the basic knowledge of grammar. Please understand well, given that the method is too important, involving more content, the following sections will be further in-depth analysis of this magical way.
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/93583645