__new__ Section 8.6 Python class method in-depth analysis: call the parent class method parameters __new__ confusion

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 )

The section "Section 8.5 Python class methods and constructors __init__ __new__-depth analysis of the relationship: the relationship between the parameters and the order of execution cases explain" through case detailed analysis of the execution order of the two methods, I do not know if you noticed, in the above code __new__ __new__ method calls the parent class method, the transmission parameters are only passed one argument cls, without the two latter parameters are passed (only the first type of parameter has a solid in this embodiment reference 10). This problem also plagued the old ape, we have to verify that, if passed all the parameters will be.
First, Case 1: Call __new__ mass participation validation object class

  1. We will __new__ section method based on the change in passing all the parameters, the class is defined as follows:
class Cir():
   def __new__(cls,*args,**kwargs):
       print("Python传递给__new__的参数:\n***cls: ",cls,"\n***args: ",args,"\n***kwargs:",kwargs)
       inst = super().__new__(cls,*args,**kwargs)  #将所有参数都传递进去
       print("__new__返回值:",inst)
       return inst

   def __init__(self,radius):
       print("In init,self的值为:",self,", radius的值为:",radius)
       self.radius = radius
  1. Define instance variables:
cir=Cir(10)
执行结果报错:TypeError: object.__new__() takes no arguments
  1. Full implementation of screenshots below: Here Insert Picture Description
  2. Case Study:
    This case is not custom-defined class parent, so the parent class is the object, call the super () .__ new__ method of execution is __new__ method object class. Case binding section normal execution of instructions, not all of the parameters, can only pass the class name to the object. __New__ Python method to pass inspection.
  1. Let's look at Python-related help documentation
执行help(object.__new__),输出内容:
Help on built-in function __new__:

__new__(*args, **kwargs) method of builtins.type instance
    Create and return a new object.  See help(type) for accurate signature.

Shots are as follows:
Here Insert Picture Description
From the above can not see the object __new__ pass a parameter.

  1. We then Python3.73 document check:
    Chinese version:
    Here Insert Picture Description

ENGLISH:
Here Insert Picture Description
From the above description can also pass multiple values. So the old ape did not understand why can not pass this place more value upon actual use, if the research clearly who take big welcome to the old ape guidance.

Second, Case 2: Calling from __new__ pass parameters to verify the definition of the parent class

  1. Remarks
    define two classes, and class Vehicle Car class, Car Vehicle derived from, Car __new__ rewrite method, verify situations:
  1. __ new__ Vehicle class does not override method;
  2. __ new__ Vehicle class overrides the method, only passes CLS;
  3. Vehicle class overrides __ new__ method, passing all parameters.
  1. Case Code:
  1. Vehicle class does not override __ new__ methods, Car override this method and call the parent class passes all parameters, perform the following screenshot:
    Here Insert Picture Description
    description or not, the old ape that this is because no __ new__ Vehicle overridden method, you will __ new__ method calls its direct parent object, leading to not work.
  2. Vehicle type and Car classes override __ new__ method, the Car class call the parent class method when all of the parameters, but only pass parameters Vehicle cls class calls the parent class method, perform the following screenshot:
    Here Insert Picture Description
    you can see the implementation of success.
  3. In another case, that is passed when the method is called the parent class Vehicle class all the parameters, the result was an error, this will not provide a screenshot

Third, the conclusion
by the above case verification can be confirmed:

  1. Calling object __ when new__ method, only wear cls parameter, otherwise it will error;
  2. Calls from a custom parent class __ new__ method defined class, you can pass all the parameters, new__ method defined in the parent class __ depends on how the parameters are defined.

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/93642448