Python\ class namespaces and Object namespaces

Source: Internet
Author: User
1 class namespaces

Creating a class creates a namespace for a class that stores all the names defined in the class, called Properties of the class

And the good general properties of the class: Data Properties and Function properties

Where the data properties of a class are shared to all objects

Print (ID (g1.camp)) #引用的地址是一样的

Print (ID (garen.camp))

The output is:

364617767096

364617767096

Where the function property of a class is bound to all objects

Print (ID (g1.attack)) #两个引用地址不一样

Print (ID (garen.attack))

The output is:

1009949719304

1009951072464

Analysis: G1.attack is the function of Garen.attack, Python's class mechanism will garen function properties attack bound to g1,g 1 equivalent to get a pointer to Garen class attack function. In addition, G1.attack () passes G1 to the first parameter of attack.

2 object (instance) namespace

Creating an Object (instance) creates a namespace for an object (instance) that holds the name of the object (instance), called the property of the object (instance).

The Obj.name will first find the name in Obj's own namespace, find it in the class, find it in the class, and look for the parent class. Throws an exception if it is not found at the end.

Python\ class namespaces and Object namespaces

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.