[Numpy-discussion] Do you find this behavior surprising?

Jaime Fernández del Río jaime.frio at gmail.com
Wed Mar 25 17:11:17 EDT 2015

On Wed, Mar 25, 2015 at 1:45 PM, Benjamin Root <ben.root at 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
  OWNDATA : True
  ALIGNED : True

This could be fixed to work properly, although it is probably not worth
worrying much.

Properties of properties are weird...

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20150325/66c49bda/attachment.html>

More information about the NumPy-Discussion mailing list