[Numpy-discussion] immutable arrays
Travis Oliphant
oliphant.travis at ieee.org
Thu Sep 21 12:24:36 EDT 2006
Martin Wiechert wrote:
> Thanks Travis.
>
> Do I understand correctly that the only way to be really safe is to make a
> copy and not to export a reference to it?
> Because anybody having a reference to the owner of the data can override the
> flag?
>
No, that's not quite correct. Of course in C, anybody can do anything
they want to the flags.
In Python, only the owner of the object itself can change the writeable
flag once it is set to False. So, if you only return a "view" of the
array (a.view()) then the Python user will not be able to change the
flags.
Example:
a = array([1,2,3])
a.flags.writeable = False
b = a.view()
b.flags.writeable = True # raises an error.
c = a
c.flags.writeable = True # can be done because c is a direct alias to a.
Hopefully, that explains the situation a bit better.
-Travis
More information about the NumPy-Discussion
mailing list