The first part of reproduced from: https://www.cnblogs.com/xueli/p/4952063.html

1. Copying Python, the difference between deep and shallow copies

In Python, an object assignment is actually a reference to an object. When you create an object and then assign it to another variable, Python does not copy the object, but only the reference to the object

There are three ways to do this,

alist=[``1``,``2``,``3``,[``"a"``,``"b"``]]

 

(1) Direct assignment, the default shallow copy passes the reference to the object, the original list changes, the assigned b will do the same change

>>> b=alist >>> print b [1, 2, 3, [‘a’, ‘b’]] >>> alist.append(5) >>> print alist; print b [1, 2, 3, [‘a’, ‘b’], 5] [1, 2, 3, [‘a’, ‘b’], 5]

(2) Copy shallow copy, no child object is copied, so the original data changes, child object will change

>>> import copy

>>> c=copy.copy(alist) >>> print alist; print c [1, 2, 3, [‘a’, ‘b’]] [1, 2, 3, [‘a’, ‘b’]] >>> alist.append(5) >>> print alist; print c [1, 2, 3, [‘a’, ‘b’], 5] [1, 2, 3, [‘a’, ‘b’]]

>>> alist[3] [‘a’, ‘b’] >>> alist[3].append(‘cccc’) >>> print alist; Print c [1, 2, 3, ‘a’, ‘b’, ‘CCCC], 5] [1, 2, 3, [‘ a’, ‘b’, ‘CCCC]] the child object is changed

 

 

(3) Deep copy, contains the copy of the object inside the original object, so changes to the original object will not cause any changes to the child elements in the deep copy

>>> import copy

>>> d=copy.deepcopy(alist) >>> print alist; Print d [1, 2, 3, [‘ a ‘, ‘b’]] of [1, 2, 3, [‘ a ‘, ‘b’]] never change > > > alist. Append (5) > > > print alist; Print d [1, 2, 3, (‘ a ‘, ‘b’), 5] [1, 2, 3, [‘ a ‘, ‘b’]] never change > > > alist [3] [‘ a ‘, ‘b’] >>> alist[3].append(“ccccc”) >>> print alist; Print d [1, 2, 3, ‘a’, ‘b’, ‘CCCCC], 5] [1, 2, 3, [‘ a’, ‘b’]] still remains unchanged

 

 

2. Background and significance of python replication, deep copy and shallow copy?

Unlike matlab, for example, b=a, you simply assign a value to B, and then how a changes, B does not change. The same is true for C.

If you want a to change as b changes, c language proposed the concept of reference, equivalent to an alias.

example:

int a; int &ra=a; // Define a reference to ra, which is a reference to variable A, alias

okay! This article explains the nature of shallow copy and deep copy: reprogramming memory for storage

www.jianshu.com/p/9ed9b5ce7…

Baijiahao.baidu.com/s?id=162735…

This article suggests that pythond’s design prevents data tampering, or flexible changes

 

In Python, we have a very popular expression for objects, everything is an object. Any data type constructed is an object, whether it is a number, a string, a function, or even a module. Python treats it as an object.

All Python objects have three attributes: identity, type, and value.

 
Copy the code
  1. name="Li J"
  2. print(type(name))
  3. print(id(name))
  4. print(name)
  5.  
  6. # output:
  7. #<type 'str'>
  8. # 140334394101408
  9. #Li J

Mutable and immutable objects

In Python, objects are divided into two broad categories, mutable and immutable, by the way they are updated.

Mutable objects: lists, dictionaries, collections. The so-called variable means that the value of the variable object is variable and the identity is unchanged.

Immutable objects: numbers, strings, tuples. An immutable object is an object whose identity and value are immutable. The newly created objects are associated with the original variable names, the old objects are discarded, and the garbage collector collects them when appropriate.

 
Copy the code
  1. Var1 ="python" # String types are immutable
  2. print(id(var1))
  3. var1="java"
  4. print(id(var1))
  5.  
  6. A =[3,4] #list is mutable,
  7. print(id(a))
  8. a.append(3)
  9. print(id(a))
  10.  
  11. # output result:
  12. 140591145210096
  13. 140591145211632
  14. 140590909362688
  15. 140590909362688

 

reference

In Python programs, each object is allocated a space in memory to hold the object. The address of the object’s location in memory is called a reference. When you develop a program, you define variable names that actually refer to the address of the object.

Reference is actually a number in memory address number, when using the object, as long as you know the address of the object, you can operate the object, but because this number address is not convenient to use and remember in the development, so use the variable name to replace the number of the object address. In Python, a variable is just a representation of an address, not a storage space.

For example, when visiting a website, the host is actually determined by the IP address, but the IP address is not easy to remember, so the domain name is used instead of the IP address. When using the domain name to visit a website, the domain name is resolved into an IP address.

A variable and a reference to it are the same thing:

 
Copy the code
  1. b=18
  2. print(id(b))
  3. print(id(18))
  4.  
  5. Output:
  6. 29413312
  7. 29413312

      

Shallow copy:

 
Copy the code
  1. Print (" shallow copy: ")
  2. import copy
  3. B = [1, 2, 3, 4, 5]
  4. print("id b:",id(b))
  5. h=copy.copy(b)
  6. print("id h",id(h))
  7. print(h)
  8. h.append(6)
  9. print(h)
  10. print("id h",id(h))
  11. Print (b) # print(b) # print(b)
  12.  
  13. B [1]='n' # after the list element changes, the new list does not change
  14. print(h)
  15.  
  16. Output:
  17. Shallow copy:
  18. ('id b:', 140165805110552)
  19. ('id h', 140165805110480)
  20. [1, 2, 3, 4, 5]
  21. [1, 2, 3, 4, 5, 6]
  22. ('id h', 140165805110480)
  23. [1, 2, 3, 4, 5]
  24. [1, 2, 3, 4, 5, 6]
 
Copy the code
  1. a = [1, 2]
  2. l1 = [3, 4, a]
  3. l2 = copy.copy(l1)
  4. print(l1)
  5. print(l2)
  6. print(id(l1))
  7. print(id(l2))
  8. a[0] = 11
  9.  
  10. print(id(l1))
  11. print(id(l2))
  12. print(l1)
  13. print(l2)
  14. Output:
  15. [3, 4, [1, 2]]
  16. [3, 4, [1, 2]]
  17. 140624327425704
  18. 140624326197400
  19. 140624327425704
  20. 140624326197400
  21. [3, 4, [11, 2]]
  22. [3, 4, [11, 2]]

 

 

You can see that it’s a shallow copy, it’s just a copy of one layer, and when you get to A, a changes, it changes.

There are many ways to implement shallow copy in Python: copy for copy modules, copy for objects, factory methods, slicing, etc. In most cases, programs are written with shallow copies unless there is a specific need; Advantages of shallow copy: High copy speed, small space, and high copy efficiency.

Deep copy

Unlike the shallow copy, which copies only top-level references, the deep copy copies layer by layer until all references are immutable.

 
Copy the code
  1. a = [1, 2]
  2. l1 = [3, 4, a]
  3. l2 = copy.deepcopy(l1)
  4. print(l1)
  5. print(l2)
  6. print(id(l1))
  7. print(id(l2))
  8. a[0] = 11
  9.  
  10. print(id(l1))
  11. print(id(l2))
  12. print(l1)
  13. print(l2)
  14.  
  15. Output:
  16. [3, 4, [1, 2]]
  17. [3, 4, [1, 2]]
  18. 140673014398488
  19. 140672779715720
  20. 140673014398488
  21. 140672779715720
  22. [3, 4, [11, 2]]
  23. [3, 4, [1, 2]]

 

Why is the default Python copy shallow copy?

Time perspective: Shallow copy takes less time;

Space Angle: Shallow copy costs less memory;

Efficiency: Shallow copy copies only top-level data and is more efficient than deep copy.

 

 

reference

www.jianshu.com/p/9ed9b5ce7…

Baijiahao.baidu.com/s?id=162735…