Let’s check how to create an immutable Numpy array.
To make the array immutable you need to set writeable flag as false.
import numpy as np my_array = np.array([1, 2, 3]) print(my_array) my_array = 5 print(my_array) my_array.flags.writeable = False my_array = 6 print(my_array)
As you can see it is not possible to change the array. Python displays assignment destination is read-only ValueError.
Traceback (most recent call last): File "C:\Users\pythoneo\PycharmProjects\pythoneoProject\immutable_array_python.py", line 11, in
my_array = 6 ValueError: assignment destination is read-only
Setting writeable flag made your immutable. To turn off read-only mode you need to set writeable flag back to true.