[Tutor] mistaken about splitting expressions over lines
eryksun
eryksun at gmail.com
Tue Jun 25 17:10:01 CEST 2013
On Tue, Jun 25, 2013 at 10:11 AM, Peter Otten <__peter__ at web.de> wrote:
>
> In older Pythons for ("alpha" "beta") the compiler would merge the two
> strings into one whereas ("alpha" + "beta") would trigger a str.__add__()
> call at runtime. Nowadays the peephole optimiser recognizes ("alpha" +
> "beta") and replaces it with a single string:
>
>>>> import dis
>>>> def f():
> ... return ("alpha" +
> ... "beta")
> ...
>>>> dis.dis(f)
> 3 0 LOAD_CONST 3 ('alphabeta')
> 3 RETURN_VALUE
Constant folding for binary operations has a length limit of 20 for sequences:
>>> dis.dis(lambda: '0123456789' + '0123456789' + '0')
1 0 LOAD_CONST 3 ('0123456789
0123456789')
3 LOAD_CONST 2 ('0')
6 BINARY_ADD
7 RETURN_VALUE
>>> dis.dis(lambda: (0,1,2,3,4,5,6,7,8,9) +
... (0,1,2,3,4,5,6,7,8,9) + (0,))
2 0 LOAD_CONST 13 ((0, 1, 2, 3, 4,
5, 6, 7, 8, 9,
0, 1, 2, 3, 4,
5, 6, 7, 8, 9))
3 LOAD_CONST 14 ((0,))
6 BINARY_ADD
7 RETURN_VALUE
More information about the Tutor
mailing list