The execution sequence example for instantiating class in Python is detailed

  • 2020-12-13 19:01:22
  • OfStack

preface

This article focuses on the execution order of Python instantiation of class, without further elaboration, let's start with a detailed introduction

What is the order in which classes are instantiated in Python

Generally, a class has class variables and methods. For example, we define a class named A


class A():
 bar = "my lover love me"
 
 def __init__(self, name):
  print('A the class' ,self.__class__, name)

We've defined 1 class variable, bar, and 1 constructor with each class variable, per 16en__, so what happens if we instantiate A()! Don't worry, just listen to me...

First,python calls the built-in type class, correctly the type we use to test the reference type, and then type calls the built-in metaclass of mateClass, with mateClass calling the method with ___ 25en__, and this finishes step 1 This instance will then initialize its own class variables by scanning itself from beginning to end, After that, enter the constructor and initialize your own instance variables. [

Note: Class variables and instance variables are not the same in python,
Class variables: accessible without instantiation.
Instance variables: Are created dynamically. It must be instantiated before it can be accessed, because it didn't exist before.

]

Take the following example: do not instantiate access class variables


class A():
 a = 2
print(A.a)

Output:

[

> > > 2

]

All that said, code up. Take a look at how class inheritance works:


class A():
 def __init__(self, name):
  print('A the class' ,self.__class__, name)
  
class B(A):
 def __init__(self, name):
  self._name = name
  A.__init__(self, name)
  print('B the class', self.__class__, name)
 print('this is B class')
  
class C(B):
 def __init__(self, name):
  B.__init__(self, name)
  print('C the 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
C class

]

Let's explain one wave by itself

Start with the instantiation of class C(), sweeping 1 from start to finish, and then enter the construct of C(), meeting the constructor of the parent class C(), B. Enter class B(), scan 1 from beginning to end, and execute print('this is B class') The statement then goes to the construction of B() and meets the constructor of the parent class B(), A. Enter class A(), sweep it through and enter the construction of A(). Then, A. Executes and eject the stack, and class A() executes and eject. Back to class B(), from where we left off last time print('B的class', self.__class__, name) Keep going. Then, B. ___ executes and ejects the stack, and class B() executes and ejects the stack. Go back to class C() from where you left off last time print('C的class') Keep going. Each ___ gets executed and ejected from the stack, and class C() gets executed and ejected from the stack. The program is finished. Since class C() is instantiated, self above refers to instances of class C() rather than class A() or class B(). So let's see it by self. ___ <class '__main__.C'> Rather than <class '__main__.A'> or <class '__main__.B'> .

I will use the type keyword to dynamically create the class knowledge points. I will use the English level of CET3.5 to translate the description of type in the official documents to you.

[

Using 3 arguments, returns 1 object of new type. This is actually the dynamic form of the class statement. The name string is the class name and becomes the attribute of succes132EN__; A primitive tuple lists the base class and becomes > __bases__ properties; And the dict dictionary is the namespace containing the class body definition and is copied to the standard dictionary to become the attribute of SUCCdict__.

]

How, is not very mouthful, is not a capital meng *. so, above code, the following two ways of output 1 sample is output: overwrite name method 1


class X():
 a = 1
 def __name__(self):
 return ' rewrite name methods ' 
x =X()
print(x.__name__(), x.a)

X = type(' rewrite name methods ', (object,), dict(a = 1))
x = X()
print(X.__name__, x.a)

When type dynamically creates an instantiation, the first argument is equivalent to overwriting the method of the class with SUCCname__. S 149en but not called X, per 150en__, ha, anti-human
Fortunately, we are not so perverted, we usually define the two by the same name, as follows: X
X = type('X', (object,), dict(a = 1))

conclusion


Related articles: