Do you find this behavior surprising?

import numpy as np a = np.arange(10) flags = a.flags flags C_CONTIGUOUS : True F_CONTIGUOUS : True OWNDATA : True WRITEABLE : True ALIGNED : True UPDATEIFCOPY : False flags.writeable = False a.flags C_CONTIGUOUS : True F_CONTIGUOUS : True OWNDATA : True WRITEABLE : False <--- WTF!!?? ALIGNED : True UPDATEIFCOPY : False
I understand why this is happening, and that there is no other obvious way to make a.flags.writeable = False work than to have the return of a.flags linked to a under the hood. But I don't think this is documented anywhere, and wonder if perhaps it should. Jaime -- (\__/) ( O.o) ( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes de dominación mundial.

I fail to see the wtf. flags = a.flags So, "flags" at this point is just an alias to "a.flags", just like any other variable in python "flags.writeable = False" would then be equivalent to "a.flags.writeable = False". There is nothing numpy-specific here. a.flags is mutable object. This is how Python works. Ben Root On Wed, Mar 25, 2015 at 4:36 PM, Jaime Fernández del Río < jaime.frio@gmail.com> wrote:
import numpy as np a = np.arange(10) flags = a.flags flags C_CONTIGUOUS : True F_CONTIGUOUS : True OWNDATA : True WRITEABLE : True ALIGNED : True UPDATEIFCOPY : False flags.writeable = False a.flags C_CONTIGUOUS : True F_CONTIGUOUS : True OWNDATA : True WRITEABLE : False <--- WTF!!?? ALIGNED : True UPDATEIFCOPY : False
I understand why this is happening, and that there is no other obvious way to make
a.flags.writeable = False
work than to have the return of a.flags linked to a under the hood.
But I don't think this is documented anywhere, and wonder if perhaps it should.
Jaime
-- (\__/) ( O.o) ( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes de dominación mundial.
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

On Wed, Mar 25, 2015 at 1:45 PM, Benjamin Root <ben.root@ou.edu> wrote:
I fail to see the wtf.
flags = a.flags
So, "flags" at this point is just an alias to "a.flags", just like any other variable in python
"flags.writeable = False" would then be equivalent to "a.flags.writeable = False". There is nothing numpy-specific here. a.flags is mutable object. This is how Python works.
Ben Root
Ah, yes indeed. If you think of it that way it does make all the sense in the world. But of course that is not what is actually going on, as flags is a single C int of the PyArrayObject struct, and a.flags is just a proxy built from it, and great coding contortions have to be made to have changes to the proxy rewritten into the owner array. I guess then the surprising behavior is this other one, which was the one I (wrongly) expected intuitively:
a = np.arange(10) flags = a.flags a.flags.writeable = False flags C_CONTIGUOUS : True F_CONTIGUOUS : True OWNDATA : True WRITEABLE : True ALIGNED : True UPDATEIFCOPY : False
This could be fixed to work properly, although it is probably not worth worrying much. Properties of properties are weird... Jaime

Ah, *that* example is surprising to me. Regardless of whether it is a C int of the PyArrayObject struct or not, the way it is presented at the python code level should make sense. From my perspective, a.flags is a mutable object of some sort. Updating it should act like a mutable object, not some other magical object that doesn't work like anything else in python. Ben Root On Wed, Mar 25, 2015 at 5:11 PM, Jaime Fernández del Río < jaime.frio@gmail.com> wrote:
On Wed, Mar 25, 2015 at 1:45 PM, Benjamin Root <ben.root@ou.edu> wrote:
I fail to see the wtf.
flags = a.flags
So, "flags" at this point is just an alias to "a.flags", just like any other variable in python
"flags.writeable = False" would then be equivalent to "a.flags.writeable = False". There is nothing numpy-specific here. a.flags is mutable object. This is how Python works.
Ben Root
Ah, yes indeed. If you think of it that way it does make all the sense in the world.
But of course that is not what is actually going on, as flags is a single C int of the PyArrayObject struct, and a.flags is just a proxy built from it, and great coding contortions have to be made to have changes to the proxy rewritten into the owner array.
I guess then the surprising behavior is this other one, which was the one I (wrongly) expected intuitively:
a = np.arange(10) flags = a.flags a.flags.writeable = False flags C_CONTIGUOUS : True F_CONTIGUOUS : True OWNDATA : True WRITEABLE : True ALIGNED : True UPDATEIFCOPY : False
This could be fixed to work properly, although it is probably not worth worrying much.
Properties of properties are weird...
Jaime
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
participants (2)
-
Benjamin Root
-
Jaime Fernández del Río