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

Benjamin Root ben.root at ou.edu
Wed Mar 25 17:34:52 EDT 2015


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 at gmail.com> wrote:

> 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
>   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 at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20150325/9cb19eff/attachment.html>


More information about the NumPy-Discussion mailing list