I was reading this question, about immutable numpy arrays, and in a comment to one of the answers someone shows that the given trick does not work when y = x[:]
is used rather than y = x
.
>>> import numpy as np
>>> x = np.array([1])
>>> y = x
>>> x.flags.writeable = False
>>> y[0] = 5
Traceback (most recent call last):
File "<pyshell#42>", line 1, in <module>
y[0] = 5
ValueError: assignment destination is read-only
>>> del x, y
>>> x = np.array([1])
>>> y = x[:]
>>> x.flags.writeable = False
>>> y[0] = 5
>>> x
array([5])
(Python 3.7.2, numpy 1.16.2)
What even is the difference between these two and why do they behave so differently in this specific case?
EDIT: this does not answer my question because it only asks about the situation using lists, I want to know why the numpy ndarray shows this peculiar behavior where depending on the method of copying modifying the data sometimes does and sometimes doesn't raise an error.