'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)
2 0 LOAD_FAST 0 (x)
3 LOAD_CONST 6 (604800)
6 BINARY_ADD
7 RETURN_VALUE
There's absolutely no run-time cost to writing it out, and you get to
be flexible with whitespace and such, which you can't do with
literals.
ChrisA
More information about the Python-list
mailing list