[Numpy-discussion] Curious behavior of __radd__

Charles R Harris charlesr.harris at gmail.com
Wed Feb 1 14:53:15 EST 2012


On Wed, Feb 1, 2012 at 12:26 PM, Andreas Kloeckner
<lists at informa.tiker.net>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.)
>
>
It gets called once for every scalar in the array, or in your case, just
the scalar, and the scalars are converted to python types for the call. At
least, that is what it looks like.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20120201/311f3819/attachment.html>


More information about the NumPy-Discussion mailing list