[Python-Dev] Don't set local variable in a list comprehension or generator (original) (raw)

Benjamin Peterson benjamin at python.org
Wed May 18 15:41:48 CEST 2011


2011/5/18 Victor Stinner <victor.stinner at haypocalc.com>:

Hi,

''.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: STOREFAST(c), LOADFAST(c). The variable is not available outside the list comprehension/generator. I would like to remove the variable in these cases to speed up (micro-optimize!) Python. 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?

Far more useful would be figuring out how to remove the call.

-- Regards, Benjamin



More information about the Python-Dev mailing list