A question about understanding the python class and a question about the python class
Today, I read a python sub-article translated by the python tribe. It is very enlightening and recorded:
1 print ('A') 2 class Person (object): 3 print ('B') 4 def _ int _ (self, name ): 5 print ('C') 6 self. name = name 7 print ('D') 8 print ('E') 9 10 11 p1 = Person ('name1') 12 p2 = Person ('name2 ')
Output result:
ABDECC
First, we need to understand that, 1. The running rules of the python program are executed in one row and one row in sequence. 2. There are differences between the class and function running Modes in python. The function is executed only when it is called (that is, the internal code is run) and is not executed during definition. The class has already executed a line of internal code during definition, not during instantiation. This is why A and B are printed first. When the code runs to _ int _, the class definition does not call this function, so it is not executed. Therefore, the output is D and E. When p1 and p2 are instantiated, the _ int _ function is called to execute the internal code of the function and print C and C.