Understanding Python assignment and copying

零到壹度
Release: 2018-04-19 15:24:02
Original
1019 people have browsed it

The examples in this article describe understanding Python assignment and copying. Share it with everyone for your reference, the details are as follows:

Variables and Assignment

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 ==,isWhat 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
Copy after login

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
Copy after login

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.

Understanding Python assignment and copying

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]
Copy after login

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]
Copy after login

Understanding Python assignment and copying

Object copy

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 thecopymodule.


Copy is divided into shallow copy and deep copy.

>>> s = [1,2,3] >>> sc = copy.copy(s) # 浅拷贝 >>> sc [1, 2, 3]
Copy after login
>>> sdc = copy.deepcopy(s) # 深拷贝
>>> sdc
[1, 2, 3]
Copy after login

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
Copy after login

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
Copy after login

Understanding Python assignment and copying

拷贝出来的对象 a 中的元素引用的是 x 和 y,当你修改 x 的值,a 也会跟着变。

>>> b = copy.deepcopy(z) # 深拷贝
>>> b[0] is z[0]
False
Copy after login

Understanding Python assignment and copying

对于深拷贝,里面的元素也重新拷贝了一份,拷贝了一份与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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!