'complex' function with string argument.

Chris Angelico rosuav at gmail.com
Mon Mar 17 20:32:18 CET 2014

```On Tue, Mar 18, 2014 at 6:22 AM, Marko Rauhamaa <marko at pacujo.net> wrote:
> Chris Angelico <rosuav at gmail.com>:
>
>> On Tue, Mar 18, 2014 at 5:15 AM, Marko Rauhamaa <marko at pacujo.net> wrote:
>>> 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.
>
> The point is, you don't need to "philosophize" about complex literals
> when even negative numbers don't have literals in Python.

Ah! I get you.

The difference between literals and constants is one that almost never
matters, though. Python may not have a syntax for negative or complex
literals, but it does have notations for various sorts of constants,
which function the same way. (Literals are by definition constants.)
So Python may not have a convenient notation for "number of seconds in
a week" (unless you work with DNS and find the bare integer 604800
convenient), but you can write 7*24*60*60 and it's just as good in a
function:

>>> ast.dump(ast.parse("7*24*60*60"))
'Module(body=[Expr(value=BinOp(left=BinOp(left=BinOp(left=Num(n=7),
op=Mult(), right=Num(n=24)), op=Mult(), right=Num(n=60)), op=Mult(),
right=Num(n=60)))])'
>>> def f(x):
return x + 7*24*60*60

>>> dis.dis(f)