Mark Dickinson <dicki...@gmail.com> added the comment: > 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 ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue15099> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com