[Numpy-discussion] Behaviour of ndarray and other objects with __radd__?
Jonathan Taylor
jonathan.taylor at utoronto.ca
Wed Jun 15 19:21:15 EDT 2011
Ok. I suspected it was something of that sort and that certainly
makes sense to have this ability. Can we perhaps add this to the
documentation/specification so that we can more confident that this
behavior remains into the future?
Thanks,
Jonathan.
On Wed, Jun 15, 2011 at 11:46 AM, Charles R Harris
<charlesr.harris at gmail.com> wrote:
>
>
> On Wed, Jun 15, 2011 at 9:34 AM, Olivier Delalleau <shish at keba.be> wrote:
>>
>> I don't really understand this behavior either, but juste note that
>> according to
>> http://docs.scipy.org/doc/numpy/user/c-info.beyond-basics.html
>> "This attribute can also be defined by objects that are not sub-types of
>> the ndarray"
>>
>> -=- Olivier
>>
>> 2011/6/15 Jonathan Taylor <jonathan.taylor at utoronto.ca>
>>>
>>> Hi,
>>>
>>> I would like to have objects that I can mix with ndarrays in
>>> arithmetic expressions but I need my object to have control of the
>>> operation even when it is on the right hand side of the equation. I
>>> realize from the documentation that the way to do this is to actually
>>> subclass ndarray but this is undesirable because I do not need all the
>>> heavy machinery of a ndarray and I do not want users to see all of the
>>> ndarray methods. Is there a way to somehow achieve these goals?
>>>
>>> I would also very much appreciate some clarification of what is
>>> happening in the following basic example:
>>>
>>> import numpy as np
>>> class Foo(object):
>>> # THE NEXT LINE IS COMMENTED
>>> # __array_priority__ = 0
>>> def __add__(self, other):
>>> print 'Foo has control over', other
>>> return 1
>>> def __radd__(self, other):
>>> print 'Foo has control over', other
>>> return 1
>>>
>>> x = np.arange(3)
>>> f = Foo()
>>>
>>> print f + x
>>> print x + f
>>>
>>> yields
>>>
>>> Foo has control over [0 1 2]
>>> 1
>>> Foo has control over 0
>>> Foo has control over 1
>>> Foo has control over 2
>>> [1 1 1]
>>>
>>> I see that I have control from the left side as expected and I suspect
>>> that what is happening in the second case is that numpy is trying to
>>> "broadcast" my object onto the left side as if it was an object array?
>>>
>>> Now if I uncomment the line __array_priority__ = 0 I do seem to
>>> accomplish my goals (see below) but I am not sure why. I am
>>> surprised, given what I have read in the documentation, that
>>> __array_priority__ does anything in a non subclass of ndarray.
>>> Furthermore, I am even more surprised that it does anything when it is
>>> 0, which is the same as ndarray.__array_priority__ from what I
>>> understand. Any clarification of this would be greatly appreciated.
>>>
>
> There's a bit of code in the ufunc implementation that checks for the
> __array_priority__ attribute regardless of if the object subclasses ndarray,
> probably because someone once needed to solve the same problem you are
> having. The comment that goes with it is
>
> /*
> * FAIL with NotImplemented if the other object has
> * the __r<op>__ method and has __array_priority__ as
> * an attribute (signalling it can handle ndarray's)
> * and is not already an ndarray or a subtype of the same type.
> */
>
> Chuck
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
More information about the NumPy-Discussion
mailing list