[issue24800] Incorrect handling of local variables in comprehensions with exec()

Peter Eastman report at bugs.python.org
Wed Aug 5 22:33:59 CEST 2015


Peter Eastman added the comment:

I don't believe that explanation is correct.  You can just as easily get the same problem without explicitly passing a map to exec().  For example:

def f():
    script = """
print(a)
print([a for i in range(5)])
    """
    a = 5
    exec(script)
    
f()

The documentation for exec() states, "In all cases, if the optional parts are omitted, the code is executed in the current scope."  Therefore the code above should be exactly equivalent to the following:

def f():
    a = 5
    print(a)
    print([a for i in range(5)])
    
f()

But the latter works and the former doesn't.  Contrary to the documentation, the code is clearly not being executed in the same scope.

----------
resolution: not a bug -> 
status: closed -> open

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


More information about the Python-bugs-list mailing list