[issue38331] Exec not recognizing global variables inside function

Terry J. Reedy report at bugs.python.org
Sat Oct 5 02:23:39 EDT 2019


Terry J. Reedy <tjreedy at udel.edu> added the comment:

Ronald is correct, and for the reason given.  Python functions are lexically scoped, not dynamically scoped, and you are expecting the latter.  The exec global and local namespaces are used to resolve identifier in the code you pass.  Your first example passes 'func' and that is resolved to the *pre-compiled* function object which uses is lexicographical globals.  It does not contain the func-local 'var'.  You second example contains a def statement to be compiled in the exec namespaces.  Please post to python-list if you want further discussion.

----------
nosy: +terry.reedy
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

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


More information about the Python-bugs-list mailing list