1、Object reference pass value or pass reference
Object assignments in Python are actually simple object references. That is, when you create an object and then assign it to another variable, Python doesn't copy the object, it copies the reference to the object. This approach is equivalent to a combination of value passing and reference passing. If a function receives a reference to a mutable object (such as a dictionary or a list), it can modify the original value of the object - this is equivalent to assigning a value by "passing by reference". If the function receives a reference to an immutable variable (such as a number, string, or ancestor), it can't modify the original object directly - this is equivalent to assigning a value by "value passing".
Let's look at an example of number passing first:
>>> def test(a): ... print id(a) ... a = a + 1 ... print id(a) ... return a ... >>> b =19 >>> id(b) 38896272 >>> c = test(b) 38896272 38896260 >>> id(b) 38896272 >>> b 19
The id function obtains the memory address of an object.
Obviously from the above example, you can see that the b variable was passed as a parameter to the test function, a reference to b was passed, and the address of b was passed, so the address of the variable a obtained within the function is the same as the address of the variable b. However, within the function, the assignment operation was performed on a, and the value of a changed from 19 to 20, and in fact, the memory space occupied by both 19 and 20 still exists, and a points to the memory where 20 is located. After the assignment operation, a points to the memory where 20 is located. After the assignment, a points to the memory where 20 is located, and b still points to the memory where 19 is located, so when b is printed later, its value is still 19.
In addition, about the id of the integer variable, all the integers in the range of [-5,256], python is allocated space in advance in the array initialized, so if two variables are the same small integer, the object is the one that is initialized at the very beginning, so the id of the two variables is the same.
All integers outside the range [-5,256] will be created one at a time, so the id will change.
>>> a = 256 >>> id(a) 43340980 >>> b = 256 >>> id(b) 43340980 # a and b have the same id >>> a = 257 >>> id(a) 44621040 >>> b = 257 >>> id(b) 44620908 # a and b have different ids >>> a = -5 >>> id(a) 43338160 >>> b = -5 >>> id(b) 43338160 >>> a = -6 >>> id(a) 44621184 >>> b = -6 >>> id(b) 44621112
Look at another example of list passing:
>>> def test(a): ... print id(a) ... a[0] = 100 ... print id(a) ... return a ... >>> b = [7,8,9,10] >>> id(b) 46408088 >>> c = test(b) 46408088 46408088 >>> id(b) 46408088 >>> b [100, 8, 9, 10]
As you can see from the above example, the b variable was passed as a parameter to the test function, a reference to b was passed, and the address of b was passed, so the address of variable a obtained within the function is the same as the address of variable b. However, within the function, an assignment operation was performed on a, and the value of a[0] changed from 7 to 100, but the id of a did not change, and it is still the same as the address of variable b. So when b is printed later, the value of b[0] also changed from 7 to 100. But the id of a has not changed, it is still the same as the address of variable b. So when b is printed later, the value of b[0] also changes from 7 to 100.
2. On variable and immutable variables:
Here mutable immutable means whether that piece of content (value) in memory can be changed or not
Immutable variables:
number: int, float, str, tuple. --means that its parts (e.g., element, attribute can't be changed) can't be changed; not that the whole is immutable. Also, all Python variables are objects. int is also an object.
>>> a = 10000 >>> id(a) 46573412 >>> a = 10000000 >>> id(a) 46573460 #id changed after numeric variable reassignment >>> s = 'abc' >>> s[1] = d An element in a #string variable cannot be changed. Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' object does not support item assignment >>> id(s) 39103328 >>> s = 'ttt' >>> id(s) The id changed after the #string variable was reassigned 46425368
As you can see from the above example, the ids of numeric variables and character variables change after reassignment. This is because the assignment of an immutable variable changes the value of the immutable variable by applying for a new area in memory, storing the new value in that area, and then changing the reference of the immutable variable to point to the new area of memory.
variable
class, class instance;listings,dict,
Example 1. Assignment of Elements in Variable Variables
>>> list = [1,2,3] >>> id(list) 45486568 >>> for i in list: ... print id(i) 40207208 40207196 40207184 >>> list[0] = 0 >>> id(list) 45486568 # The id of the variable hasn't changed # >>> for i in list: ... print id(i) 40207220 # The id of the element has changed 40207196 40207184 precedent2.Assignment of Variable Variables >>> list = [1,2,3] >>> id(list) 43783392 >>> list =[2,3,5] >>> id(list) # The id of the variable has changed 44454296
As you can see from the example above, the re-assignment of an element in the list will not change the id of the entire list, but the id of that element will undergo that birth. Because what is stored in the list is actually references to individual elements, the result of assigning a value to that element is that the reference to the element changes.
In short, whether variable or immutable variables, as long as the entire variable is assigned, Python applies a new area in memory, stores the new value in that area, and then changes the reference of the immutable variable to point to the new area of memory; if an element in the variable variable is assigned, the detachment results in a change in the element and not in a change in the parent object.
3. Deep Copy Vs Shallow Copy
() shallow copy
() deep copy
A shallow copy is a newly created object of the same type as the original object, but its contents are references to elements of the original object. The copied object itself is new, but the contents are not. If the elements of the original object contain a list, dict, or object instead of a basic data structure, then a change in the contents of the list, dict, or object by either the original object or the copied object will cause both to change.
Deep copy is an exact copy of the original object, including copies of subobjects inside the object, so the copy object and the original object are completely independent of each other, and changes to either side will not have any effect on the other.
>>> import copy >>> list = [1, 2, [3, 4]] >>> copy_list = (list) >>> deepcopy_list = (list) >>> >>> id(list) 44454296 >>> id(copy_list) 44515736 >>> id(deepcopy_list) 44455736 >>> >>> for k in list: ... print id(k) 43338088 43338076 44430120 >>> for k in copy_list: ... print id(k) 43338088 43338076 44430120 # The contents of the copy object are exactly the same as the original object >>> for k in deepcopy_list: ... print id(k) 43338088 43338076 44457456 # The contents of the deepcopy object are different from the original: the ids of the list elements are different; the ids of the numeric elements are the same, because all references to variables with the same number are the same. >>> >>> list[2][0] = 30 >>> list [1, 2, [30, 4]] >>> copy_list [1, 2, [30, 4]] # A change in an element in a child of the original object causes the same change in the copy object >>> deepcopy_list [1, 2, [3, 4]] # Changes to elements in a child object of the original object do not cause the same changes to occur in the deepcopy object.
Above this Python's object passing and the use of the Copy function in detail is all I have to share with you, I hope to be able to give you a reference, and I hope you support me more.