The difference between an assignment in Python, a shallow copy, and a deep copy

Source: Internet
Author: User
Tags shallow copy

1. First of all, for the object to be manipulated, for non-mutable objects, such as string, number, tuple, and so on, these three operations are equivalent, are reference

Import Copy

A= ' Apple '
B=a
C=copy.copy (a)
D=copy.deepcopy (a)
Print (ID (a))
Print (ID (b))
Print (ID (c))
Print (ID (d)

Output:

1840734496
1840734496
1840734496
1840734496

It can be seen that all four variables point to the same memory address, the address where the string "Apple" is located

2. For mutable objects (or container objects), such as [],{}, class instances, etc., the assignment is still a reference; a shallow copy will be reborn as the outermost container, then a reference to the inside object; a deep copy recursively creates all the container objects and then references all the immutable objects

Import Copy

a=[1,2,[4,5,[6,7]]
B=a
C=copy.copy (a)
D=copy.deepcopy (a)

Print (ID (a))
Print (ID (b))
Print (ID (c))
Print (ID (d))
Print ('-------------')

Print (ID (a[2)))
Print (ID (b[2)))
Print (ID (c[2)))
Print (ID (d[2)))
Print ('-------------')

Print (ID (a[2][2)))
Print (ID (b[2][2)))
Print (ID (c[2][2)))
Print (ID (d[2][2)))
Print ('-------------')

Print (ID (a[2][2][0)))
Print (ID (b[2][2][0)))
Print (ID (c[2][2][0)))
Print (ID (d[2][2][0)) output: 1485397847560
1485397847560 #赋值引用了容器
1485397847432 #浅拷贝生成了新的容器
1485397847368 #深拷贝生成了新的容器
-------------
1485397821768
1485397821768
1485397821768 #浅拷贝引用了二级容器
1485397847304 #深拷贝生成了新二级容器
-------------
1485397847752
1485397847752 #赋值引用了三级容器
1485397847752 #浅拷贝引用了三级容器
1485397847240 #深拷贝生成了新三级容器
-------------
1840734528
1840734528 #赋值引用了不可变对象
1840734528 #浅拷贝引用了不可变对象
1840734528 #深拷贝引用了不可变对象

Summarize:

1. The assignment action is the same for all objects, both reference

2. The difference between a shallow copy and a deep copy is meaningful on a container object, as its name means, a shallow copy will only be reborn as the outermost container, and a deep copy will recursively generate all the containers

3. For immutable objects, these three operations are equivalent, all references, and only one immutable object exists in real memory

Add:

1. The actual use of shallow copy operations includes three classes: Factory functions, such as list (), slices, and copy.copy ()

2. Deep copy is only copy.deepcopy ()

The difference between an assignment in Python, a shallow copy, and a deep copy

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.