[Numpy-discussion] Using __complex__ for complex dtype

Robert Bradshaw robertwb@math.washington....
Fri Jul 10 01:52:47 CDT 2009


Nevermind, I just found http://bugs.python.org/issue1675423 .

On Jul 9, 2009, at 1:41 AM, Robert Bradshaw wrote:

> I know using __complex__ has been discussed before, but it would be  
> really nice if it were at least used to convert object to the  
> complex dtypes.
>
> - Robert
>
>
> In [1]: class Foo:
>    ...:     def __float__(self): return 2.0
>    ...:
>    ...:
>
> In [2]: import numpy
>
> In [3]: numpy.array([Foo()])
> Out[3]: array([<__main__.Foo instance at 0x17b6da0>], dtype=object)
>
> In [4]: numpy.array([Foo()], dtype=float)
> Out[4]: array([ 2.])
>
> In [5]: numpy.array([Foo()], dtype=complex)
> Out[5]: array([ 2.+0.j])
>
> In [6]: class FooC:
>    ...:     def __complex__(self): return 1+1j
>    ...:
>    ...:
>
> In [7]: numpy.array([Foo()])
> Out[7]: array([<__main__.Foo instance at 0x17b6f08>], dtype=object)
>
> In [8]: numpy.array([FooC()], dtype=complex)
> ---------------------------------------------------------------------- 
> -----
> TypeError                                 Traceback (most recent  
> call last)
>
> /Users/robert/<ipython console> in <module>()
>
> TypeError: a float is required
>



More information about the NumPy-Discussion mailing list