This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author mark.dickinson
Recipients docs@python, johnf, mark.dickinson
Date 2012-06-18.12:50:13
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1340023814.34.0.385085789347.issue15099@psf.upfronthosting.co.za>
In-reply-to
Content
> As you can see from this example, the exec'uted code *does* call the 
> instance's overloaded __getitem__ and __missing__ methods when outside a 
> function, but doesn't when inside.

Yep;  that's because the 's' and 'f' lookups at top level are *local* lookups, and the 's' lookup from inside the body of 'f' is done as a *global* lookup (as explained in the docs here: [1]).  In the exec statement, the locals can be any mapping-like object.  The behaviour's a bit clearer if you pass separate globals and locals dictionaries:

>>> source = """\
... print s
... def f():
...     print s
... f()
... """
>>> locals = {'s': 1729}
>>> globals = {'s': 31415}
>>> exec source in globals, locals
1729
31415


[1] http://docs.python.org/reference/executionmodel.html#interaction-with-dynamic-features
History
Date User Action Args
2012-06-18 12:50:14mark.dickinsonsetrecipients: + mark.dickinson, johnf, docs@python
2012-06-18 12:50:14mark.dickinsonsetmessageid: <1340023814.34.0.385085789347.issue15099@psf.upfronthosting.co.za>
2012-06-18 12:50:13mark.dickinsonlinkissue15099 messages
2012-06-18 12:50:13mark.dickinsoncreate