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

Georg Brandl report at bugs.python.org
Fri Feb 13 11:16:04 CET 2009


Georg Brandl <georg at python.org> added the comment:

Ezio: this happens inside a function, like this:

def f():
    canBusType = 'CANdiag'
    result = [eval('canBusType') for i in range(3)]

This is expected, and won't easily fix.  The reason is that list
comprehensions in 3.x use a function namespace "under the hood" (in 2.x,
they were implemented like a simple for loop). Because inner functions
need to know what names to get from what enclosing namespace, the names
referenced in eval() can't come from enclosing functions. They must
either be locals or globals.

Of course, the question to the OP is why eval() is needed anyway.

----------
nosy: +georg.brandl
resolution:  -> wont fix
status: open -> pending

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


More information about the Python-bugs-list mailing list