'complex' function with string argument.

Ian Kelly ian.g.kelly at gmail.com
Wed Mar 19 11:33:34 CET 2014

On Wed, Mar 19, 2014 at 4:09 AM, Marko Rauhamaa <marko at pacujo.net> wrote:
> wxjmfauth at gmail.com:
>> Le mercredi 19 mars 2014 09:51:20 UTC+1, Marko Rauhamaa a écrit :
>>> wxjmfauth at gmail.com:
>>> >>>> complex(2, 1+1j)
>>> > (1+1j)
>>> I find it neat, actually.
>>>>> # tricky: yes, neat: no
>>>>> complex(1+1j, 2)
>> (1+3j)
> So complex(a, b) is documented to produce a+bj when a and b are integers
> or floats. What's more natural than saying it produces a+bj when a and b
> are complex numbers? It's a straightforward generalization that in no
> way violates the more limited documentation.

When is it ever useful though?  I only see a use for passing a as
complex if b is omitted, and I don't see any use for passing b as
complex.  If there's no use case, then it's just a confusing edge case
that will catch unsuspecting programmers who thought the data they
were passing in was real-valued when actually it wasn't.  It would be
better to raise an exception in either of the cases above, in my
opinion.  If you really want to form a complex a+bj from two other
complex numbers, there is always the explicit (a + b * 1j).

That said, complex numbers have been around since 1.4 or so, and
there's probably not much chance to change it now.

More information about the Python-list mailing list