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
name="Li J"
print(type(name))
print(id(name))
print(name)
# output:
#<type 'str'>
# 140334394101408
#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
Var1 ="python" # String types are immutable
print(id(var1))
var1="java"
print(id(var1))
A =[3,4] #list is mutable,
print(id(a))
a.append(3)
print(id(a))
# output result:
140591145210096
140591145211632
140590909362688
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
b=18
print(id(b))
print(id(18))
Output:
29413312
29413312
Shallow copy:
Copy the code
Print (" shallow copy: ")
import copy
B = [1, 2, 3, 4, 5]
print("id b:",id(b))
h=copy.copy(b)
print("id h",id(h))
print(h)
h.append(6)
print(h)
print("id h",id(h))
Print (b) # print(b) # print(b)
B [1]='n' # after the list element changes, the new list does not change
print(h)
Output:
Shallow copy:
('id b:', 140165805110552)
('id h', 140165805110480)
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6]
('id h', 140165805110480)
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6]
Copy the code
a = [1, 2]
l1 = [3, 4, a]
l2 = copy.copy(l1)
print(l1)
print(l2)
print(id(l1))
print(id(l2))
a[0] = 11
print(id(l1))
print(id(l2))
print(l1)
print(l2)
Output:
[3, 4, [1, 2]]
[3, 4, [1, 2]]
140624327425704
140624326197400
140624327425704
140624326197400
[3, 4, [11, 2]]
[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
a = [1, 2]
l1 = [3, 4, a]
l2 = copy.deepcopy(l1)
print(l1)
print(l2)
print(id(l1))
print(id(l2))
a[0] = 11
print(id(l1))
print(id(l2))
print(l1)
print(l2)
Output:
[3, 4, [1, 2]]
[3, 4, [1, 2]]
140673014398488
140672779715720
140673014398488
140672779715720
[3, 4, [11, 2]]
[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…