The examples in this article describe understanding Python assignment and copying. Share it with everyone for your reference, the details are as follows:
In Python, everything is an object, and objects pass through "variables" "name" reference, "variable name" is more accurately called "name", just like each of us has his own name, we refer to a person by name, and an object is referred to by name in the code.
Variable assignment is to bind a name to the object.Assignment does not copy the object.Just like our parents give us a name when we are born, giving someone a nickname does not create an extra person, it just creates an extra name.
There are two ways to compare two objects, namely: is and ==,is
What is compared is whether the two objects are the same. Whether they are the same can be identified by the ID value of the object. Object,==
compares whether the values of the two objects are equal
>>> x1 = [1,2]
>>> x2 = [1,2]
>>> x1 is x2
False
>>> id(x1)
4338854088
>>> id(x2)
4338904392
>>> x1 == x2
True
Although the values of x1 and x2 are the same, they are two independent and different objects in the memory, occupying different The memory space is like two identical apples placed on the table, but they are actually two different objects.
>>> x3 = x2
>>> x3 is x2
True
>>> id(x3)
4338904392
As mentioned before, assignment is to bind a name to an object. Here we just bind a new name to the object corresponding to x2 called x3. This is like putting an apple on the table. At first it was labeled x2, and later it was labeled x3. It is essentially the same apple, so x2 and x3 actually refer to the same object.
When the object is modified through x2, x3 will also change because they are essentially the same object. This is like when Zhang San and Xiao Zhang are the same person, Adding clothes to Zhang San is actually adding clothes to Xiao Zhang.
>>> x2.append(3)
>>> x2
[1, 2, 3]
>>> x3
[1, 2, 3]
However, when I reassign x2, it means that x2 no longer refers to the previous object, but refers to the new object, and x3 still refers to the previous object. For example, a big apple on the table has two labels: x2 and x3. Reassigning a value to x2 is equivalent to attaching the x2 label to another apple, but x3 is still attached to the old apple.
>>> x2 = [3, 4]
>>> x3
[1, 2, 3]
Sometimes in business we need to copy an object, but we don’t want to have side effects on the original object, definitely It cannot be solved by assigning a value to a new variable (because assignment is not a copy of the object), so Python specifically provides a copy mechanism to quickly create an object with the same value based on the original object. This functionality is provided by thecopy
module.
Copy is divided into shallow copy and deep copy.
>>> s = [1,2,3] >>> sc = copy.copy(s) # 浅拷贝 >>> sc [1, 2, 3]
>>> sdc = copy.deepcopy(s) # 深拷贝
>>> sdc
[1, 2, 3]
The copied objects only have the same value, but are actually different objects
>>> s == sc == sdc
True
>>> s is sc
False
>>> s is sdc
False
So what is the difference between shallow copy and deep copy?
For immutable objects, such as integers, strings, tuples, and collection objects composed of these immutable objects, there is no difference between shallow copy and deep copy, both copy a new object
两者的区别在于拷贝组合对象,比如列表中还有列表,字典中还有字典或者列表的情况时,浅拷贝只拷贝了外面的壳子,里面的元素并没有拷贝,而深拷贝则是把壳子和里面的元素都拷贝了一份新的。
来看一个例子:
>>> x = [2, 3]
>>> y = [7, 11]
>>> z = [x, y]
>>> a = copy.copy(z) # 浅拷贝
>>> a[0] is z[0]
True
拷贝出来的对象 a 中的元素引用的是 x 和 y,当你修改 x 的值,a 也会跟着变。
>>> b = copy.deepcopy(z) # 深拷贝
>>> b[0] is z[0]
False
对于深拷贝,里面的元素也重新拷贝了一份,拷贝了一份与x和y等值的两个元素,修改 x 和 y 的值,不会对 b 产生影响
对列表的切片拷贝z[:]
或者是调用对象的copy方法list.copy()
都属于浅拷贝。对于自定义对象,我们还可以自己实现__copy__
方法和__deepcopy__
方法
相关阅读:
Django入门实践指南-第1部分
厉害了,10行代码实现抽奖助手自动参与抽奖
看了很多代码,却依然无从下手?
The above is the detailed content of Understanding Python assignment and copying. For more information, please follow other related articles on the PHP Chinese website!