In the tutorial of Numpy, there a section called Copies and Views, as a newbie of Python, I was shocked. In particular, some priori information of Matlab aggravate the feeling even more. Let’s take a look at what happened in Python when I want to copy the value of one variable to another one.
1 No Copy at All
Naturally, when I execute
b=a in any language.
b is expected to have the
a , and if
b are both array of
int , then after
b=b+1 will make
b greater than
this is not the case in Python.
import numpy as np a = np.arange(12) b = a
The statement of
b is a will return
True which means that
two names for the same array object.
a behave like the pointer in C.
if you execute
b = 555 ,then
a will be
Also, if you use the
id(b) will return the same
id() function return the unique identifier of an object. If two
objects have the same identifier, the two objects are actually one object.
2 view or Shallow Copy
view is a good word for what shallow copy mean. For a large value, if you
want to change part of it, you view the part you want to change.
import numpy as np a = np.random.random((3,5))
If you just want to change the second and the third column of
a, you view the
part you want change by slicing it:
p = a[:,1:3] p[:] = 10
Then the second and third column will be all
view behaves like the
microscope. Only the part of viewed will be shared by the two objects.
3 Deep Copy
What? deep copy? Yes, it’s deep copy that implement the real copy as we expect.
b = a.copy()
b is a complete copy of
a will not share the same
object id. This is what the copy we want.
For example, if you have:
import numpy as np a = np.arange(4) b = a.copy() b = 4
you will have
[0,1,2,3] . Now
4 only happened in array
Fortunately, the three version copy just happened on array type. when
a are just integers or float numbers,
= will implement the real
For example, when you have:
a = 2 b = a b += 2
then you get