It seems like python (2.6) is not able to optimize this simple temp variable 'a' ?
I use to assign a local variable to some code in order to reduce the line length.
To me this is a simple optimization any correct compiler can do automatically.
from dis import dis
def f(func):
func()
def functioncall():
print ' => function called'
def unoptimized():
print 'in unoptimized'
a = functioncall
f(func=a)
def optimized():
print 'in optimized'
f(func=functioncall)
unoptimized()
optimized()
print 'dis(unoptimized)'
dis(unoptimized)
print 'dis(optimized)'
dis(optimized)
Output :
in unoptimized
=> function called
in optimized
=> function called
dis(unoptimized)
10 0 LOAD_CONST 1 ('in unoptimized')
3 PRINT_ITEM
4 PRINT_NEWLINE
11 5 LOAD_GLOBAL 0 (functioncall)
8 STORE_FAST 0 (a)
12 11 LOAD_GLOBAL 1 (f)
14 LOAD_CONST 2 ('func')
17 LOAD_FAST 0 (a)
20 CALL_FUNCTION 256
23 POP_TOP
24 LOAD_CONST 0 (None)
27 RETURN_VALUE
dis(optimized)
15 0 LOAD_CONST 1 ('in optimized')
3 PRINT_ITEM
4 PRINT_NEWLINE
16 5 LOAD_GLOBAL 0 (f)
8 LOAD_CONST 2 ('func')
11 LOAD_GLOBAL 1 (functioncall)
14 CALL_FUNCTION 256
17 POP_TOP
18 LOAD_CONST 0 (None)
21 RETURN_VALUE
Why it cannot remove this automatically?
11 5 LOAD_GLOBAL 0 (functioncall)
8 STORE_FAST 0 (a)