Deep and shallow copy tutorial in programming language python

Deep and shallow copy tutorial in programming language python In python, the equal sign refers to the object correspondi...
Deep and shallow copy tutorial in programming language python

In python, the equal sign refers to the object corresponding to the object address

Data in python is divided into variable and immutable types:

  • Variable type: list, dictionary
  • Immutable data types: String, Float, Int, Tuple.

For immutable type data, its memory address is unchanged

# For example: id(3) # View data memory address by id # Output: 140708771369856 # Check the memory address of 3 again, and you can see that it is the same as the memory address of the previous 3 id(3) # Output: 140708771369856

For variable type data, its memory address is not fixed

Its extend, append, + = and other operations will change the data corresponding to its address.

# For example: id([1, 2]) # Output: 2204932119304 # Looking at the same data again, you can see that its memory address has changed id([1, 2]) # Output: 2204931521416 # Therefore, for immutable type data, deep replication and shallow replication are the same and have no impact a = 3 b = a print(f'a=, a Memory address:\nb=, b Memory address:') # Output: a=3, a Memory address: 140708771369856 b=3, b Memory address: 140708771369856

For variable type data

  • Different variable data objects have different memory addresses even if the values are equal
  • If you want to reference the values of other objects, it is best to use deep or shallow copy
# If no deep or shallow copy is performed: a = [1, 2, 3, ['a', 'b']] b = a print(f'a: \na Memory address:\nb: \nb Memory address:') print('*' * 20) # Change the value of a a += b # The memory address of a remains unchanged, but the data changes. Note: the changes here are only for operations such as append, + =, extend, etc. If it is directly assigned, such as a=[0, 6], it will not be affected. print(f'a: \na Memory address:\nb: \nb Memory address:') # Output: a: [1, 2, 3, ['a', 'b']] a Memory address: 2204931464136 b: [1, 2, 3, ['a', 'b']] b Memory address: 2204931464136 ******************** a: [1, 2, 3, ['a', 'b'], 1, 2, 3, ['a', 'b']] a Memory address: 2204931464136 b: [1, 2, 3, ['a', 'b'], 1, 2, 3, ['a', 'b']] b Memory address: 2204931464136

Shallow replication

  • import copy is required in python2
  • You can use copy() directly in Python 3, but the deepcopy() method also needs to import modules
  • The copy() method is called shallow copy and the deep copy() method is called deep copy
  • Shallow copy is to copy the new object and the public reference data address of the original object.

Suggestion: when there is enough memory, select deep copy, so that the logical processing is independent, will not have the impact of context, and is not prone to difficult bug s.

Case 1:

a = [1, 2, 3, ['a', 'b']] b = a.copy() # Shallow replication a += b # Changing a has no effect on the value of b print(f'a: \na Memory address:\nb: \nb Memory address:') # Output: a: [1, 2, 3, ['a', 'b'], 1, 2, 3, ['a', 'b']] a Memory address: 2204931416328 b: [1, 2, 3, ['a', 'b']] b Memory address: 2204932131208

Case 2:

''' No one answers the problems encountered in learning? Xiaobian created a Python Learning exchange group: 725638078 Look for like-minded friends to help each other,There are also good videos and tutorials in the group PDF e-book! ''' a = [1, 2, 3, ['a', 'b']] b = a.copy() print(f'a: \na Memory address:\nb: \nb Memory address:') print('*' * 20) a += b a[3] += ['d', 'e'] # Changing a affects the value of b because b print(f'a: \na Memory address:\nb: \nb Memory address:') # Output: a: [1, 2, 3, ['a', 'b']] a Memory address: 2204931366024 b: [1, 2, 3, ['a', 'b']] b Memory address: 2204931363976 ******************** a: [1, 2, 3, ['a', 'b', 'd', 'e'], 1, 2, 3, ['a', 'b', 'd', 'e']] a Memory address: 2204931366024 b: [1, 2, 3, ['a', 'b', 'd', 'e']] b Memory address: 2204931363976

Deep replication

  • In scenario 2 of shallow replication, changing the nested list in a will also change the value of b.
  • In order to avoid this situation, if the memory meets the requirements, try to use deep copy.
  • Deep copy is to completely generate new objects and reference new memory addresses. There is no connection between the two data.
import copy a = [1, 2, 3, ['a', 'b']] b = copy.deepcopy(a) a += b a[3] += ['d', 'e'] print(f'a: \na Memory address:\nb: \nb Memory address:') # Output: a: [1, 2, 3, ['a', 'b', 'd', 'e'], 1, 2, 3, ['a', 'b']] a Memory address: 2204930530952 b: [1, 2, 3, ['a', 'b']] b Memory address: 2204930568

At the end, I recommend a very good learning tutorial for you. I hope it will be helpful for you to learn Python!

Python basics tutorial recommendation

Python crawler case tutorial recommendation

Article source: https://www.cnblogs.com/djdjdj123/p/15529731.html

Baidu online disk searchwww.bdsoba.com Search whitenesswww.sobd.cc Brother Chengwww.jcdi.cn Search the code www.somanba.cn

10 November 2021, 19:49 | Views: 1840

Add new comment

For adding a comment, please log in
or create account

0 comments