[Numpy-discussion] Curious behavior of __radd__
Travis Oliphant
travis at continuum.io
Thu Feb 2 00:58:58 EST 2012
This seems odd to me. Unraveling what is going on (so far):
Let a = np.complex64(1j) and b = A()
* np.complex64.__add__ is calling np.add
* np.add(a, b) needs to search for an "add" loop that matches the input types and it finds one with signature
('O', 'O') -> 'O'
* a is converted to an array via the equivalent of a1 = array(a,'O')
* b is converted to an array in the same way b1 = array(b, 'O')
Somehow a1 as an array of objects is an array of "complex" types instead of "complex64" types
Then the equivalent of a1[()] + b1[()] is called.
So, the conversion is being done by
a1 = array(a, 'O')
I don't know why this is. This seems like a regression, but I don't have an old version of NumPy to check.
compare:
type(a)
type(np.array(a,'O')[()])
These should be the same type. But they are not...
-Travis
On Feb 1, 2012, at 1:26 PM, Andreas Kloeckner wrote:
> Hi all,
>
> here's something I don't understand. Consider the following code snippet:
>
> ---------------------------------------------------
> class A(object):
> def __radd__(self, other):
> print(type(other))
>
> import numpy as np
> np.complex64(1j) + A()
> ---------------------------------------------------
>
> In my world, this should print <type 'numpy.complex64'>.
> It does print <type 'complex'>.
>
> Who is casting my sized complex to a built-in complex, and why?
>
> It can be Python's type coercion, because the behavior is the same in
> Python 3.2. (And the docs say Python 3 doesn't support coercion.)
>
> (Please cc me.)
>
> Thanks,
> Andreas
> _______________________________________________
> 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