The execution order python has always been very troublesome, simply with the mind of some
1, the normal sequence.
print(1) a = 2 l = [3]
This is not attached to the results, can guess, this order execution, as well as the double equal sign equal sign this, both from the right execution, the implementation of the right of the equal sign, the assignment to the left of the equal sign.
2, execution of the function
# coding=utf-8 def a(): print('is a') def b(): a() print('b use a') def c(): b() print('c use b') def d(): c() print('d use c') d()
There is a total of four defined function, when you call d (), but also with the c (), and so on, the output can imagine. Is shown in
result:
is a
b use a
c use b
d use c
3, class more trouble, magic previous execution method __new__ __init__ method, the specific example of the execution order
# coding=utf-8 class A: def __init__(self): print('init a') param = 1 print(param) class B: def __new__(cls, *args, **kwargs): print('new b') class C: def __init__(self): print('init c') def __new__(cls, *args, **kwargs): print('new c') A() B() C()
Guess it runs directly results first. . . What will happen
Ha ha ha, the result is:
1
INIT a
new b
new c
Draw, custom variables> magic method __new __> __ before performing init__ method, we also found out why the new-exist with init, run only the new, but it did not print init?
Science about the new:
__new __ () is used to create an instance, the method is called before the instance is created, it is a class-level method is also a static method;
Science about init:
__init __ () has at least one parameter of self, __ new __ () Returns instance passed to init, is passed to self, __ init __ () to complete the operation on the basis of some initialization __new __ () on.
Comparative Results:
__new __ () must have a return value, return an instance of the object;
__init __ () no return value.
__new__
Must have a return value, out of the returned instance instantiated, __init__
a parameter self, is the __new__
return of example, __init__
the __new__
complete other initialization operation based on the __init__
no return value
If __new __ () creates an instance of the current class, automatically invokes __init __ () function, __new __ () through a return call parameters cls to ensure that the current class instance, if other class class name, then create the return of other class instance, it does not call the current class __init __ () function.
He would want to perform need to add return super (C, cls) .__ new __ (cls) under the new method
Here know is not too much to understand.
4, a small running instance of the class
class A: def __init__(self,name): print('A class',A.__class__,name) class B(A): def __init__(self,name): self.name = name A.__init__(self,name) print("B class",B.__class__,name) # print('this is B') class C(B): def __init__(self,name): B.__init__(self,name) print("c class") c = C('jery')
Here will be how to enforce it? ? First guess
The result is
A class <class 'type'> jery
B class <class 'type'> jery
c class
Combined with his understanding of more in-depth look.
in conclusion:
1) __new__ method performed before __init__ method;
2) __new__
must have a return value, out of the returned instance instantiated, instance to init, init then performed again, if the object is not instantiated, will not init execution;
3) function as well as other follow line by line execution.