'complex' function with string argument.

Chris Angelico rosuav at gmail.com
Mon Mar 17 14:59:50 EDT 2014


On Tue, Mar 18, 2014 at 5:15 AM, Marko Rauhamaa <marko at pacujo.net> wrote:
>>> This  '3 + 2j'  is not a number, its an algebraic sum.
>>>
>>> This '3+2j' is a complex number. Ok, maybe not, but its closer to what
>>> we expect (I'm sorry, but I like i instead of j )
>>
>>Hmm. That's a pretty tricky distinction.
>
> Is "-2.0" a literal?
>
> What's the outcome of
>
>    -2.0.__str__()

If you mean (-2.0).__str__(), then it returns '-2.0', but that proves
nothing. Lots of objects have a str() which isn't a literal. Closer to
what you're talking about would be repr(), but even then, it doesn't
prove that something's a literal. The easiest way to tell is probably
ast.parse():

>>> ast.dump(ast.parse("-2.0"))
'Module(body=[Expr(value=UnaryOp(op=USub(), operand=Num(n=2.0)))])'

It's an expression consisting of unary minus and the float literal 2.0.

ChrisA



More information about the Python-list mailing list