Thứ Bảy, 9 tháng 2, 2019

PL 101: Python numpy assignment operation is very amusing.

Hi all,
In this post, I just found that Python numpy is really an amusing language in the assignment operation and the mutable property of strings and tuples. I will continuously update this page to reflect my surprise on the features of Python numpy package.

First, the assignment operation is not a deep copy operation. It means that it just copy the pointer of contents to a new variable name. For example,
In [1]: import numpy as np
In [2]: a = np.zeros((2,3))
In [3]: a
Out[3]:
array([[0., 0., 0.],
[0., 0., 0.]])
In [4]: b = a
In [5]: b[0][0]= 1
In [6]: b
Out[6]:
array([[1., 0., 0.],
[0., 0., 0.]])
In [7]: a
Out[7]:
array([[1., 0., 0.],
[0., 0., 0.]])

You can see that modifying matrix `b` will cause matrix `a` modified.

Happy new year 2019, the year of Pig! Happy Python.