complex() bug or feature?

Emile van Sebille emile at fenx.com
Thu Nov 8 06:44:29 EST 2001


"Carsten Gaebler" <clpy at snakefarm.org> wrote in message
news:3BEA6446.8D8B4012 at snakefarm.org...
> Hi there,
>
> is this the way complex() is supposed to work?
>
> Python 2.2b1 (#2, Oct 22 2001, 10:47:30)
> [GCC 2.95.2 20000220 (Debian GNU/Linux)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> complex('1', '1')
> (1+0j)
> >>>
>
> If complex() allows stings as arguments (like float()), what's
> happened to the imaginary part?
>

It looks like you never get what you'd expect when you pass in a string.

>>> complex('3',1)
(3+0j)
>>> complex(3,1)
(3+1j)
>>> complex('3','1')
(3+0j)
>>> complex(3,'1')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: complex() arg can't be converted to complex

But it looks like you can work around it using complex(float(i), float(j)).

But you probably already figured that out, which leaves us both wondering
why it doesn't raise an error.  ;-)

--

Emile van Sebille
emile at fenx.com

---------'




More information about the Python-list mailing list