[issue5242] eval() function in List Comprehension doesn't work

Tim Gordon report at bugs.python.org
Fri Feb 13 14:01:06 CET 2009


Tim Gordon <tim.py at aleph17.co.uk> added the comment:

If you know what variable you are going to be eval-ing, or at least, 
have a list of those that might be eval-ed, you can get around this 
issue by making sure they are explicitly referenced in the inner scope 
(i.e., in the list comprehension).  For example, even though list 
comprehensions work in 2.x, generator expressions don't, but this hack 
does (on 2.4 at least):

def f():
  canBusType = 'CANdiag'
  return (eval('canBusType') for i in range(3) if True or canBusType)

By putting a semantically vacuous reference to canBusType (and any 
other variables you want) you make sure they are usable from within the 
eval as well.

----------
nosy: +QuantumTim

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue5242>
_______________________________________


More information about the Python-bugs-list mailing list