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

How to parse assignment, shallow copy and deep copy in Python

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces how to analyze the assignment, shallow copy and deep copy in Python. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

Let's make a few points clear.

Immutable type: the value in memory specified by the data type object cannot be changed.

(1), when changing the value of an object, because the value in its memory can not be changed, it will copy the original value and then change it, so it will open up a new memory space to store the new value.

Mutable type: the value above the address specified by the object of this data type can be changed.

(1), after the variable is changed, the value above the memory address it points to is changed directly, no copy behavior occurs, no new memory address behavior occurs, and the space will not be re-opened up.

Variable: an element of a system table that has a connection space to an object

Object: a piece of memory allocated to store the value it represents

Reference: is an automatically formed pointer from a variable to an object

When writing in the code editor

Test = 'python'

The python interpreter will

1. Create the variable test

two。 Create an object (allocate a piece of memory) to store python values

3. Pass the pointer between the variable and the object

Linked together, the connection from the variable to the object is called a reference (variable reference object)

Assignment

Copying references to new objects will not open up new memory space

Shallow copy

Copying the parent object does not copy the child objects within the object.

1) A shallow copy means that the copy is only a reference to the original object element, in other words, the object produced by the shallow copy is new, but its content is not new, it is just a reference to the original object.

2) shallow copy means that the address value of the stored variable is passed to the assigned value, and the last two variables refer to the same address.

Deep copy

A deep copy copies all the elements of the object, including multiple layers of nested elements. The deeply copied object is a completely new object and no longer has anything to do with the original object. All changes to the original assigned object will not affect the new object that has already been copied.

Examples of deep and shallow copies of immutable objects

Import copytest = 'abc'print ("= assignment =") b=testprint (test) print (b) print (id (test)) print (id (b)) print ("= shallow copy =") b=copy.copy (test) print (test) print (b) print (id (test) print (id (b) print ("= deep copy =") b=copy.deepcopy (test) print (test) print (b) print (id (test) print (id (b))

Result

= assign value =

Abc

Abc

1855476943856

1855476943856

= shallow copy =

Abc

Abc

1855476943856

1855476943856

= Deep copy =

Abc

Abc

1855476943856

1855476943856

The results can be obtained from the above:

The saying that the immutable type has not been copied, the id address is the same anyway

For example of variable object depth copy

Import copytest= ("= assignment =") b=testprint (test) print (b) print (id (test)) print (id (b)) print ("= shallow copy =") b=copy.copy (test) print (test) print (b) print (id (test)) print (id (b) print ("= deep copy =") b=copy.deepcopy (test) print (test) print (b) print (id (test)) print (id (b))

Result

= assign value =

[1, 2, 3]

[1, 2, 3]

2461497814592

2461497814592

= shallow copy =

[1, 2, 3]

[1, 2, 3]

2461497814592

2461497661824

= Deep copy =

[1, 2, 3]

[1, 2, 3]

2461497814592

2461497661888

Import copya = [1,2,3,4, ['await,' b']] # original object b = a # assignment, pass the reference of the object c = copy.copy (a) # object copy, shallow copy d = copy.deepcopy (a) # object copy Deep copy a.append (5) # modify object aa [4] .append ('c') # modify the array object print ('a =', a) print ('b =', b) print ('c =', c) print ('d =', d) in object a

Result

('a =', [1, 2, 3, 4, ['a', 'baked,' c'], 5])

('b =', [1, 2, 3, 4, ['await,' baked,'c'], 5])

('c =', [1, 2, 3, 4, ['await,' baked,'c']])

('d =', [1, 2, 3, 4, ['await,' b']])

1. When an element is added in the outer layer, the shallow copy does not change with the change of the original list; when the element is added in the inner layer, the shallow copy changes.

two。 The deep copy remains the same no matter how the source list changes

3. The copied object changes with the original list

On how to parse the Python assignment, shallow copy and deep copy to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Development

Wechat

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

12
Report