[Numpy-discussion] Using __complex__ for complex dtype

Lisandro Dalcin dalcinl@gmail....
Fri Jul 10 16:48:49 CDT 2009


On Fri, Jul 10, 2009 at 3:52 AM, Robert
Bradshaw<robertwb@math.washington.edu> wrote:
> Nevermind, I just found http://bugs.python.org/issue1675423 .
>

Nevermind? Perhaps NumPy should handle this gotcha for Python < 2.6 ?

-

> 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
>>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>



-- 
Lisandro Dalcín
---------------
Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC)
Instituto de Desarrollo Tecnológico para la Industria Química (INTEC)
Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET)
PTLC - Güemes 3450, (3000) Santa Fe, Argentina
Tel/Fax: +54-(0)342-451.1594


More information about the NumPy-Discussion mailing list