Message163099
> 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 |
|
Date |
User |
Action |
Args |
2012-06-18 12:50:14 | mark.dickinson | set | recipients:
+ mark.dickinson, johnf, docs@python |
2012-06-18 12:50:14 | mark.dickinson | set | messageid: <1340023814.34.0.385085789347.issue15099@psf.upfronthosting.co.za> |
2012-06-18 12:50:13 | mark.dickinson | link | issue15099 messages |
2012-06-18 12:50:13 | mark.dickinson | create | |
|