[Python-Dev] Scope object (Re: nonlocals() function?) (original) (raw)

Tobias Ivarsson thobes at gmail.com
Tue Apr 6 13:50:48 CEST 2010


On Tue, Apr 6, 2010 at 11:13 AM, Greg Ewing <greg.ewing at canterbury.ac.nz>wrote:

Reid Kleckner wrote:

If I remember correctly, the exec statement is going away in py3k, and calling exec() with one argument can modify the local scope.

I've been kind of wondering what the deal is with exec in py3. I always thought the reason for making exec a statement was so that locals optimisation could be turned off in its presence, so I'm not sure how py3 is getting away with making it a function.

It looks like py3 does not allow exec to modify the locals:

$ python3 Python 3.1.1 (r311:74543, Aug 24 2009, 18:44:04) [GCC 4.0.1 (Apple Inc. build 5493)] on darwin Type "help", "copyright", "credits" or "license" for more information.

def x(a): ... exec(a) ... return a ... x("a = 5") 'a = 5' # the above statement would have returned 5 if the locals had been modified

Anyhow, it seems to me that as long as locals() or whatever might replace it is able to find the existing value of a local, it should also be able to change that value, wherever it happens to be stored. I suppose that might fail if an optimiser decides to keep multiple copies of a local for some reason, though. But even if it has to be read-only, I still think a view object would be a more py3ish way of handling locals() and the like. You might only want access to a few locals, in which case building a dict containing all of them would be wasteful. -- Greg

-- Greg


Python-Dev mailing list Python-Dev at python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/thobes%40gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20100406/ed6abe8d/attachment-0001.html>



More information about the Python-Dev mailing list