Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between deep copy and shallow copy in python

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

What is the difference between deep copy and shallow copy in python? I believe many inexperienced people are at a loss about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Both the deep copy and the shallow copy are copies of the object, which will produce an object that looks the same. The essential difference between them is whether the address of the copied object is the same as that of the original object, that is, the difference between address replication or value replication.

What are mutable objects and what are immutable objects:

A mutable object means that an object can modify the value in the address it points to without changing the address it points to.

An immutable object means that the value of the address pointed to by an object cannot be modified. if you modify the value of the object, the address it points to will change, which is equivalent to copying a copy of the value pointed to by the object. then make the modification and save it to another address, but the mutable object will not do this action, but will directly change the value on the address pointed to by the object. And this object still points to this address.

What you need to pay attention to in both deep and shallow copies is the copy of variable elements:

In a shallow copy, the address of the copied new object is different from that of the original object, but the address of the variable element in the new object (such as the list) is the same as that in the original object, that is to say, the shallow copy copies the shallow data structure (immutable element), and the variable element in the object is not copied into the new address as a deep data structure. Instead, it points to the same address as the variable element in the original object, so when you modify the variable element in the new object or the original object, the two objects change at the same time, but the deep copy will not do so. this is the most fundamental difference between a shallow copy and a deep copy.

Examples are as follows:

# encoding=utf-8

Import copy

A = [1, 2, 3, 4, 5, ['axiomagery]]

# original object

B=a# assignment, passing references to objects

C=copy.copy (a) # object copy, shallow copy

D=copy.deepcopy (a) # object copy, deep copy

Print "a =", a, "id (a) =", id (a), "id (a [5]) =", id (a [5])

Print "b =", b, "id (b) =", id (b), "id (b [5]) =", id (b [5])

Print "c =", c, "id (c) =", id (c), "id (c [5]) =", id (c [5]))

Print "d =", d, "id (d) =", id (d), "id (d [5]) =", id (d [5]))

Print "*" * 70

A.append (6) # modify object a

A [5] .append ('c') # modify the array object in object a

Print "a =", a, "id (a) =", id (a), "id (a [5]) =", id (a [5])

Print "b =", b, "id (b) =", id (b), "id (b [5]) =", id (b [5])

Print "c =", c, "id (c) =", id (c), "id (c [5]) =", id (c [5]))

Print "d =", d, "id (d) =", id (d), "id (d [5]) =", id (d [5]))

Results:

From the results of the program, lists an and b are assignment operations, two objects point to the same address, and an and b are two references to the same address, which is actually the same thing, so when one object modifies a shallow element (immutable) or a deep element (mutable), the other object is changing at the same time.

C is an object generated by a shallow copy. You can see that the overall id of a (or b) and c are different, but the address of the fifth element-list is the same (pointing to the same address), so c does not increase when b adds an element at the shallow element level (immutable), but the fifth element of b-list adds an element. The fifth element of c also increases, because the fifth element of b and c-the list points to the same address, and the value on this address changes, changing at the same time in both places.

If you look at the addresses of the shallow (immutable) and deep-seated elements (variable) of dforce d are not the same as those of ajournal bpenc, therefore, no matter how it is modified, d will not change. This is the result of deep copy.

It can also be understood like this:

Deep copy has nothing to do with the past, and no matter how the original object is changed, the current object will not be affected.

Shallow copy, if the list element of the original object changes, it will change the current object, and if the list element in the current object changes, it will also affect the original object.

A shallow copy is a broken lotus root.

Deep copy is divorce.

Usually, a deep copy is used when copying, because after a shallow copy, the immutable objects in the two objects point to different addresses and will not change each other, but the variable elements in the two objects point to the same address, one changes, and the other changes at the same time, which will have an impact (list is a mutable object).

What if you want to make the original list and copy list unaffected?

With a deep copy, the copy completely opens up a new memory address to save the previous object, although the content of the possible address may be the same (the same address, such as's'), but will not affect each other.

For example:

List1= ['axiajiajiao', 'Bengyun' [']

List2= ['axiajiajiao', 'Bengyun' [']

The address of'a'in both lists is the same

Id (list1 [0]) = id (list2 [0]), but the addresses of the two lists are different

Generally speaking, immutable elements include:

Int,float,complex,long,str,unicode,tuple

After reading the above, have you mastered the difference between deep copy and shallow copy in python? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report