Python references and objects in a detailed

Source: Internet
Author: User
Tags shallow copy

Python references and objects in a detailed

@[(Flying Elephant)

The variable name and object are separated in Python

Example 1:

a = 1
This is a simple assignment statement, the integer 1 is an object, A is a reference, and an assignment statement is used, and reference a points to object 1.

Example 2:
>>> 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.

Example 3:
>>> 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.

Example 4:
>>> a = 4>>> b = a>>> id(a)36151568>>> id(b)36151568>>> 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.

Example 5:
>>> L1 = [1,2,3]>>> L2 = L1>>> id(L1)139643051219496>>> id(L2)139643051219496>>> L1[0] = 10>>> id(L1)139643051219496>>> id(L2)139643051219496>>> L2[10, 2, 3]

Similarly, as in the fourth example, the value of one of the objects is modified, but the result is not the same as the fourth chestnut, in this experiment, the reference to L1 and L2 did not change, but the value of the list object [three-to-one] became [10,2,3] (the 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.

Example 6:
= [1,2,3]forin l:    =8    # 这里只是让item指向l[i]所指向的对象,item = 8,则item指向新的对象8,不改变l[i]print# [1,2,3]forinrange(len(l)):    =8    # 这里令l[i]指向新的对象 8print# [8,8,8]
Example 7:
L1=[]a= 0 forIinch Range(1,5): A=I L1.append (a)# Adds a point to the objectPrint(L1)# [1, 2, 3, 4]L2=[]b=[1,2,3] forIinch Range(1,5): b[1]=I l2.append (b)# Adds the object that B points to, which includes a reference to the list element, and the list itself does not change, except that the list item [1] points to an object that has changedPrint(L2)# [[1, 4, 3], [1, 4, 3], [1, 4, 3], [1, 4, 3]]# not expected [[1, 1, 3], [1, 2, 3], [1, 3, 3], [1, 4, 3]]

You can refer to Example 5. So, each time the list is actually adding the same object.

== [1,2,3]forinrange(1,5):    b[1= i    l2.append(copy.copy(b))    # l2.append(copy.deepcopy(b)) 和copy.copy()结果一样print# [[1, 1, 3], [1, 2, 3], [1, 3, 3], [1, 4, 3]]

Copy.copy () shallow copy. Only the parent object is copied, and the inner child objects of the object are not copied.

So what's the difference between copy.copy () and copy.deepcopy ()?

== [1,[4,5],3]forinrange(1,5):    b[1][0= i    # [[1, [4, 5], 3], [1, [4, 5], 3], [1, [4, 5], 3], [1, [4, 5], 3]]    # l2.append(copy.deepcopy(b)) # [[1, [1, 5], 3], [1, [2, 5], 3], [1, [3, 5], 3], [1, [4, 5], 3]]print(l2)

Copy.deepcopy () deep copy copy objects and their sub-objects

Example 8:

Probe into the parameter passing of function

def testPara(aList):    aList[0] = 8    print("inside function",aList) # [8, 2, 3]if __name__ == ‘__main__‘:    l1 = [1,2,3]    testPara(l1)    print(l1) # [8, 2, 3]
def testPara(aList):    = [1,2]    print("inside function"# [1,2] aList重新指向一个新的对象if__name__==‘__main__‘:    = [1,2,3]    testPara(l1)    print# [1, 2, 3]

To change the L1 directly within the function, you can return to the function L1

def test(l:list):    l.pop(0)if__name__==‘__main__‘:    = [1,2,3]    test(l)    print# [2,3]

Python references and objects in a detailed

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.