Can Python optimize this simple variable call?
Question: 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)
    


Answer: CPython, the standard implementation of Python, doesn't do much optimization;
it is a pretty straightforward translation of source code to bytecode. There
are a few optimizations thrown in (elimination or hoisting of code under `if
__debug__` being one of them) and I do believe there's a peephole optimizer of
some kind, but I assume the compilation phase is meant to be reasonably quick,
which precludes doing a lot of analysis.

Maintainability also seems key to the core Python developers. Here is [a
thread from 2009](http://www.gossamer-threads.com/lists/python/dev/755165) in
which a patch for dead code removal is considered, then rejected, because it
would make the compiler less maintainable.

If you want high performance, you want something other than CPython; maybe
PyPy would help. Python in general is optimized to save programmer time, not
runtime. If you don't want dead code in your bytecode, don't put it in your
source code. :-)

