References and objects in Python learning are important knowledge points that must be mastered, and today we share these two parts to help you quickly master references and object-related operations.
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 = [+]
>>> 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:
L = [n/A]
For item in L:
Item = 8
# here just let item point to the object pointed to by l[i], item = 8, then item points to the new object 8, does not change L[i]
Print (l) # [n/A]
For I in range (Len (l)):
L[i] = 8
# This makes l[i] point to the new object 8
Print (l) # [8,8,8]
Example 7:
L2 = []
b = [[+]
For I in range (1,5):
B[1] = i
L2.append (copy.copy (b))
# L2.append (copy.deepcopy (b)) and copy.copy () as a result
Print (L2) # [[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 ()?
L2 = []
b = [1,[4,5],3]
For I in range (1,5):
B[1][0] = i
L2.append (copy.copy (b)) # [[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)
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 = [A]
Testpara (L1)
Print (L1) # [8, 2, 3]
def Testpara (alist):
Alist = [up]
Print ("Inside function", alist) # [alist] re-points to a new object
if __name__ = = ' __main__ ':
L1 = [A]
Testpara (L1)
Print (L1) # [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__ ':
L = [n/A]
Test (L)
Print (l) # [2,3]
Source: Push Cool
Python Learning references and objects