Create a class in Python, execute the program when creating instance attributes, and create a python instance

Source: Internet
Author: User

Create a class in Python, execute the program when creating instance attributes, and create a python instance
For example, you can understand the execution process of the program in this way: 1 -- open up a space in the memory to store the created class object, and the Tool (Class Name) points to the memory address of the class object; this type of object stores the property num = 0 (Class Attribute) and method def _ init _ (); 2 -- the program goes down, "Tool (" Shovel ") "an object (Instance Object) is created. This instance object has a special attribute that can help the Instance Object point to the Class Object. When Python automatically executes the init method after creating the instance object, pass custom/default attributes (instance attributes) to the instance object. Then run Tool. num + = 1. Change the value of the class property from num = 0 to num = 1. The program runs here and the init method ends, at this time, the Instance Object already has the instance attribute. The program returns the reference address of the current instance object and assigns it to tool1, which means that tool1 points to this initialized instance object; 3 -- the program continues. "Tool (" ")" is executed in the same way as step 3, but Tool is executed again. num + = 1 once, the value of num = 1 is changed to num = 2; 4 -- the program continues to go down, and "Tool (" Bucket ")" is executed in the same way as step 1, but execute Tool again. num + = 1 once, change the value of num = 2 to num = 3; 5 -- the program has been executed.

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.