Terry J. Reedy <tjre...@udel.edu> added the comment:

Issues like this, about exec, have come up multiple times. I just closed #14049 
as a duplicate of this, and listed there some other issues. So I think that the 
doc for exec (and execfile in 2.7) could be better still. I would like to see 
something like the following added, whether instead of or in addition to the 
current proposal -- perhaps after "If provided, locals can be any mapping 
object." (quote from the 3.2 exec entry)

"At module level, globals and locals are the same dictionary. If one passes two 
separate objects as globals and locals, the code will be executed as if it were 
embedded in a class definition."

To me, this is friendlier and less intimidating than 'free variable' and the 
Execution model doc chapter. It summarizes the essential problem with such exec 
calls.

To illustrate, the following gives the same NameError. and for the same reason, 
as exec(code,{},{}), where code is the quoted unindented version with the class 
line deleted.

class x:
    x = 1
    def incx():
        return x+1
    print(incx())

----------
nosy: +terry.reedy
versions: +Python 2.7, Python 3.2

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue13557>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to