Discussion on the class instance property access rules of Python

Source: Internet
Author: User
In general, in Python, the access rules for class instance properties are fairly straightforward.

However, there are still some areas that are not very intuitive, especially for C + + and Java programmers.

Here, we need to understand the following areas:

1.Python is a dynamic language, and any entity can dynamically add or remove attributes.
2. A class defines a scope.
3. Class instances also introduce a scope that differs from the scope defined by the corresponding class.
4. When looking for a property in a class instance, first look in the instance's own scope, and if it is not found, then look in the scope of the class definition.
5. When assigning a value to a class instance property, it will actually add a property (if it does not already exist) to the scope defined by the class instance, without affecting the same name attribute defined in the corresponding class.

Let's look at an example to deepen our understanding of these points:
Copy the Code code as follows:


Class A:
cls_i = 0
Cls_j = {}
def __init__ (self):
self.instance_i = 0
Self.instance_j = {}


Here, we first define an instance of Class A, and then look at the scope of Class A and the scope of instance a, respectively:
Copy CodeThe code is as follows:


>>> a = A ()
>>> a.__dict__
{' Instance_j ': {}, ' Instance_i ': 0}
>>> a.__dict__
{' __init__ ':, ' __module__ ': ' __main__ ', ' cls_i ': 0, ' Cls_j ': {}, ' __doc__ ': None}

We see that there are cls_i and Cls_j in the scope of instance_i and instance_j,a in the scope of a.

Let's take a look at how the name lookup happens:

Copy the Code code as follows:


>>> a.cls_i
0
>>> a.instance_i
0

When looking for cls_i, the scope of instance a does not have it, but it is found in the scope of a, and it can be found directly in the scope of a when looking for instance_i.

What if we tried to modify the value of the Cls_i by instance a:
Copy the Code code as follows:


>>> a.cls_i = 1
>>> a.__dict__
{' Instance_j ': {}, ' cls_i ': 1, ' instance_i ': 0}
>>> a.__dict__
{' __init__ ':, ' __module__ ': ' __main__ ', ' cls_i ': 0, ' Cls_j ': {}, ' __doc__ ': None}

We can see that a has more than one cls_i property in its scope, with a value of 1, and we also notice that the value of the Cls_i property in a scope is still 0; here we actually add an instance property and not modify it to the Class property.

What if we manipulate the data in Cls_j through instance a (note not the Cls_j itself):
Copy the Code code as follows:


>>> a.cls_j[' a '] = ' a '
>>> a.__dict__
{' Instance_j ': {}, ' cls_i ': 1, ' instance_i ': 0}
>>> a.__dict__
{' __init__ ':, ' __module__ ': ' __main__ ', ' cls_i ': 0, ' Cls_j ': {' A ': ' A '}, ' __doc__ ': None}

We can see that the scope of a does not change, but there are some changes in the scope of a, and the data in Cls_j has changed.

The scope of the instance changes and does not affect other instances of the class, but the scope of the class changes, affecting all instances of the class, including the instance created earlier:
Copy the Code code as follows:


>>> a.cls_k = 0
>>> I.cls_k
0
  • Related Article

    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.