[issue43605] Eval/exec and comprehension scopes unclear in documentation

Cong Ma report at bugs.python.org
Sat Mar 27 04:26:26 EDT 2021


Cong Ma <m.cong at protonmail.ch> added the comment:

> I think it is *very* reasonable to expect that calling eval() on a string should have the exact same effect as if the code that is inside the eval had been written as part of the source code.

I don't think Python's execution model is defined this way. The documentation on execution model says:

> The eval() and exec() functions do not have access to the full environment for resolving names. Names may be resolved in the local and global namespaces of the caller. Free variables are not resolved in the nearest enclosing namespace, but in the global namespace.
> footnote: This limitation occurs because the code that is executed by these operations is not available at the time the module is compiled.

https://docs.python.org/3/reference/executionmodel.html#interaction-with-dynamic-features

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43605>
_______________________________________


More information about the Python-bugs-list mailing list