Python References and object understanding

Source: Internet
Author: User
Tags shallow copy

When I browsed the blog today, I saw this sentence: the variable name and object are separated in Python, and at the very beginning it was not reflected when I saw this sentence. Decide specifically to figure out the details of the variables and objects in Python. (In fact, I feel that reference and object separation is more appropriate)

Start thinking from the first variable:

In Python, if you want to use a variable, you do not need to declare it in advance, you just need to assign a value to the variable when you use it (this differs from the static type language such as C, which is related to the dynamic type of Python).

   Give the first chestnut a:

A = 1

This is a simple assignment statement, the integer 1 is an object, A is a reference, using an assignment statement, reference a points to object 1; Here is the analogy: this process is equivalent to "Fly kites", the variable A is the "line" in your hand, Python is the same as the root of the "line", Use references to touch and hold kites in the sky-objects.

You can see the identity of an object through Python's built-in function ID (), which is actually the memory address of the object:

    Note:

Python is the object of everything, so the function is also an object, so you can use the __doc__ method of the ID () function to see the specific description of the function:

>>> id.__doc__    "id (object)--Integer\n\nreturn The identity of an object.       This was guaranteed to be unique among\nsimultaneously existing objects. (Hint:it ' s The object's memory address.)

  

   A second chestnut:

A = 2

A = ' banana '

Using the ID () function of the first chestnut above:

>>> a = 1>>> ID (a) 24834392>>> a = ' banana ' >>> ID (a) 139990659655312

In the first statement, 2 is an integer object stored in memory, which refers to object 1 by assigning a reference to a

In the second statement, a string object ' banana ' is established in memory, the reference A is pointed to ' banana ' by assignment, and the object 1 does not have a reference to it, it will be used by Python's memory processing mechanism to release memory when I am garbage collected.

   A third chestnut:

A = 3

b = 3

See the references to variables a and B by using the function:

>>> a = 3>>> B = 3>>> ID (a) 10289448>>> ID (b) 10289448

Here you can see that these two references point to the same object, which is why? This is related to Python's memory mechanism, because for languages, frequent object destruction and build-up is particularly wasteful. So in Python, both integers and short characters, Python caches these objects for re-use.

   Fourth chestnut:

1. A = 4

2. B = A (here is the object where reference B points to reference a)

3. A = a + 2

View References by Function:

When you are done to step 2nd, look at the references to A and B:

>>> a = 4>>> B = a>>> ID (a) 36151568>>> ID (b) 36151568

You can see that both A and B point to an Integer object 4

Next, point to step 3rd:

>>> a = a+2>>> ID (a) 36151520>>> ID (b) 36151568

You can see that the reference to a has changed, but the reference to B has not changed, a, a, a, a, a, a, a, a, a, a, a, a, the 3rd, and a to a new object 6, even if multiple references point to the same object, and if a reference value changes, the reference is actually referred to Does not affect the point of the other references. In effect, each reference is independent and unaffected.

   The Fifth chestnut (this chestnut will involve variable data types and immutable data types in Python):

Before starting this chestnut, please remember to note the difference between the fourth chestnut.

1. L1 = [1, 2, 3]

2. L2 = L1

3. l1[0] = 10

View References by Function:

When you perform steps 1th and two, look at the references to L1 and L2:

>>> L1 = [1,2,3]>>> L2 = l1>>> ID (L1) 139643051219496>>> ID (L2) 139643051219496

At this point L1 and L2 have the same reference, all pointing to the [All-in-one] list object.

Next, proceed to step 3rd:

>>> L1[0] = 10>>> ID (L1) 139643051219496>>> ID (L2) 139643051219496>>> l2[10, 2, 3]

Same as the fourth chestnut, modified the value of one of the objects, but can be found that the result is not the same as the fourth chestnut, in this experiment, L1 and L2 no change in the reference, but the list object [three-way] value becomes [10,2,3] (List object changed)

In this case, we no longer assign a value to the L1 reference, but instead assign a value to the element of the table to which L1 points. As a result, L2 also changed.

What is the reason? Because the direction of L1,L2 has not changed, it still points to that table. The table is actually an object that contains multiple references (each of which is an element, such as l1[0],l1[1] ..., each of which points to an object, such as a. and l1[0] = 10 This assignment operation, does not change the direction of the L1, but to L1[0], which is a part of the Table object (an element), so all references to the object are affected.

(In contrast, our previous assignment didn't work on the object itself, only changing the reference point.) )

A list can alter the object itself (in-place change) by referencing its elements. This type of object, called a mutable data object (Mutable object), is the same type of data as the dictionary.

And like the numbers and strings before, you cannot change the object itself, only the reference point is changed, called the Immutable data object (immutable objects).

The tuple (tuple) we learned earlier, although it is possible to invoke a reference element but not to assign a value, cannot alter the object itself, so it is also considered immutable object.

        

   is keyword:

Of course, we can also want to know whether to point to the same object, we can use the Python is keyword, is to determine whether the two references refer to the same object.

Just like the fourth chestnut above when it comes to steps 1th and 2nd:

>>> a = 4  ... id (a) = 36151568>>> b =a ...   ID (b) = 36151568>>> A is b  True

When you proceed to the 3rd step:

>>> A = a + 2  ... id (a) = 36151520>>> A is b ...  id (b) = 36151568False

              

It occurred to me that the understanding of Python's deep copy and shallow copy could be verified by this, and the fifth chestnut could be used as an aid to understanding.          

  

Python References and object understanding

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.