What is the order in which classes are instantiated in Python
Generally speaking, there are class variables and methods in a class. For example, we define a class named A.
class A(): bar = "my lover love me" def __init__(self, name): print('A的class' ,self.__class__, name)
We have defined a class variable bar and a construction method in this class
__init__, then what happened when we instantiated a ()! Reader, don’t worry, listen to me slowly …
- First, python calls the built-in type class, which is the type we normally use to test the reference type. then the type calls the built-in metaclass mateClass, which is called again
__new__Method instantiates the class, completing the first step
- Then, this instance will initialize its class variable, which is to scan itself from beginning to end.
- After that, enter the construction method and initialize your own instance variables.
Note: Class variables and instance variables are different in python.
Class variable: can be accessed without instantiation.
Instance variable: dynamically created. You must instantiate it before you can access it, because it did not exist before.
For example, the following example does not instantiate an access class variable
class A(): a = 2 print(A.a) 输出： >>>2
Having said so much, code it. See how classes run when they inherit:
class A(): def __init__(self, name): print('A的class' ,self.__class__, name) class B(A): def __init__(self, name): self._name = name A.__init__(self, name) print('B的class', self.__class__, name) print('this is B class') class C(B): def __init__(self, name): B.__init__(self, name) print('C的class') if __name__ == '__main__': c = C('lee')
The output is as follows:
this is B class
A class <class ‘__main__.C’> lee
B class <class ‘__main__.C’> lee
Let’s show ourselves, explain a wave of
- First of all, class C () is instantiated and scanned from beginning to end. Then it enters the construction of c () and encounters the construction method B.__init__, which is the parent class c ().
- Enter class B (), scan it from beginning to end, execute the print(‘this is B class’) statement, then enter the construction of b (), and encounter the construction method of parent class b (), A.__init__.
- Enter class A (), scan it from beginning to end, and then enter the construction method A.__init__. Then A.__init__ completes execution and ejects the stack, and class A () completes execution and ejects the stack.
- Go back to class B (), and continue execution from the place where it was not completed last time. Then B.__init__ finishes executing and pops up the stack, and class B () finishes executing and pops up the stack.
- Go back to class C (), and continue with print(‘C’s class’) where it did not finish last time. Then C.__init__ completes execution and ejects the stack, and class C () completes execution and ejects the stack. The program has finished running.
- Since class C () is instantiated, self above refers to an instance of class C () instead of class A () or class B (). Therefore, self. _ class _ _ displays < class _ _ main _ _. c’ >, instead of < class _ _ main _ _. a’ > or < class _ _ main _ _. b’ >.
Add the knowledge point of dynamically creating classes by using the type keyword casually.On the blackboard, on the blackboard, on the blackboard, on the blackboard, on the blackboard, on the blackboard, on the blackboard, on the blackboard, on the blackboard, on the blackboard, on the blackboard, on the blackboard, on the blackboard, on the blackboard, on the blackboard, on the blackboard, on the blackboard.
Returns a new type of object using three parameters. This is actually a dynamic form of class statements. The name string is the class name and becomes the
__name__Attributes; Primitive tuples list base classes and become >
__bases__Attributes; And the dict dictionary is the namespace that contains the class body definition and is copied to the standard dictionary to become
How, is it not very difficult to say, is it not capital Meng *. So, the code above, the output of the following two writing methods is the same: rewrite the name method 1
class X(): a = 1 def __name__(self): return '重写name方法' x =X() print(x.__name__(), x.a)
X = type('重写name方法', (object,), dict(a = 1)) x = X() print(X.__name__, x.a)
When type dynamically creates an instantiation, the first parameter is equivalent to overriding the class’s
__name__Methods. Class x but
__name__Property is not called x, ah, good anti-human writing
Fortunately, we are generally not so abnormal, usually we will define the two as the same name, as follows: both are called x
X = type(‘X’, (object,), dict(a = 1))
Welcome to comment enthusiastically and put forward suggestions ~
Update here first. . . . 2018/10/09