A question about understanding the python class and a question about the python class

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.