From: Martin W. <mar...@gm...> - 2006-09-20 15:03:57
|
Hi list, I just stumbled accross NPY_WRITEABLE flag. Now I'd like to know if there are ways either from Python or C to make an array temporarily immutable. Thanks, Martin. |
From: Travis O. <oli...@ie...> - 2006-09-20 18:18:01
|
Martin Wiechert wrote: > Hi list, > > I just stumbled accross NPY_WRITEABLE flag. > Now I'd like to know if there are ways either from Python or C to make an > array temporarily immutable. > Just setting the flag Python: make immutable: a.flags.writeable = False make mutable again: a.flags.writeable = True C: make immutable: a->flags &= ~NPY_WRITEABLE make mutable again: a->flags |= NPY_WRITEABLE In C you can play with immutability all you want. In Python you can only make something writeable if you either 1) own the data or 2) the object that owns the data is itself "writeable" -Travis |
From: Martin W. <mar...@gm...> - 2006-09-21 08:13:40
|
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? Cheers, Martin On Wednesday 20 September 2006 20:18, Travis Oliphant wrote: > Martin Wiechert wrote: > > Hi list, > > > > I just stumbled accross NPY_WRITEABLE flag. > > Now I'd like to know if there are ways either from Python or C to make an > > array temporarily immutable. > > Just setting the flag > > Python: > > make immutable: > a.flags.writeable = False > > make mutable again: > a.flags.writeable = True > > > C: > > make immutable: > a->flags &= ~NPY_WRITEABLE > > make mutable again: > a->flags |= NPY_WRITEABLE > > > In C you can play with immutability all you want. In Python you can > only make something writeable if you either 1) own the data or 2) the > object that owns the data is itself "writeable" > > > -Travis > > > ------------------------------------------------------------------------- > Take Surveys. Earn Cash. Influence the Future of IT > Join SourceForge.net's Techsay panel and you'll get the chance to share > your opinions on IT & business topics through brief surveys -- and earn > cash > http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV > _______________________________________________ > Numpy-discussion mailing list > Num...@li... > https://lists.sourceforge.net/lists/listinfo/numpy-discussion |
From: Travis O. <oli...@ie...> - 2006-09-21 16:24:33
|
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 |
From: Martin W. <Mar...@mp...> - 2006-09-21 22:18:51
|
On Thursday 21 September 2006 18:24, Travis Oliphant wrote: > 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. > It does. Thanks Travis. > -Travis > > > > > > > > > > ------------------------------------------------------------------------- > Take Surveys. Earn Cash. Influence the Future of IT > Join SourceForge.net's Techsay panel and you'll get the chance to share > your opinions on IT & business topics through brief surveys -- and earn > cash > http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV > _______________________________________________ > Numpy-discussion mailing list > Num...@li... > https://lists.sourceforge.net/lists/listinfo/numpy-discussion |