[IPython-user] cannot paste indented code
Fernando Perez
Fernando.Perez at colorado.edu
Thu Jan 5 13:16:38 CST 2006
Matt Wilkie wrote:
>>Just add an 'if 1:' at the first line, and paste away, even if the code is
>>indented multiple levels (the above was a chunk twice-indented in an emacs
>>buffer).
>
>
> thanks for that! This is very quick and easy and, for me, negates the
> desire for more magic on top of what is already a growing pile. :)
And notice how the 'if 1' is optimized away at the bytecode level, so you
don't even lose precious performance :)
In [9]: print src
if 1:
x = 1
In [10]: dis.dis(compile(src,'<>','single'))
2 0 JUMP_FORWARD 4 (to 7)
3 JUMP_IF_FALSE 10 (to 16)
6 POP_TOP
3 >> 7 LOAD_CONST 0 (1)
10 STORE_NAME 0 (x)
13 JUMP_FORWARD 1 (to 17)
>> 16 POP_TOP
>> 17 LOAD_CONST 1 (None)
20 RETURN_VALUE
In contrast, 'if True' is NOT optimized away:
In [11]: dis.dis(compile(src.replace('if 1','if True'),'<>','single'))
2 0 LOAD_NAME 0 (True)
3 JUMP_IF_FALSE 10 (to 16)
6 POP_TOP
3 7 LOAD_CONST 0 (1)
10 STORE_NAME 1 (x)
13 JUMP_FORWARD 1 (to 17)
>> 16 POP_TOP
>> 17 LOAD_CONST 1 (None)
20 RETURN_VALUE
Cheers,
f
More information about the IPython-user
mailing list