[Python-Dev] Don't set local variable in a list comprehension or generator
victor.stinner at haypocalc.com
Wed May 18 14:21:55 CEST 2011
''.join(c for c in 'abc') and ''.join([c for c in 'abc']) do create a
temporary c variable. In this case, the variable is useless and requires
two opcodes: STORE_FAST(c), LOAD_FAST(c). The variable is not available
outside the list comprehension/generator.
I would like to remove the variable in these cases to speed up
Remove the variable breaks code using introspection like:
list([locals()['x'] for x in range(3)])
We may detect the usage of introspection (I don't know how hard it is),
only optimize trivial cases (like "x for x in ..."), or only optmize
with Python is running in optimize mode (python -O or python -OO).
What do you think? Is it useless and/or stupid?
I heard about optimization in the AST tree instead of working on the
bytecode. What is the status of this project?
More information about the Python-Dev