Is this possible: scoping question with exec

Bengt Richter bokr at accessone.com
Thu Aug 23 23:20:35 CEST 2001


On Thu, 23 Aug 2001 15:34:12 -0500, Brian Kelley <bkelley at wi.mit.edu> wrote:

>Running Python 2.1
>
>def a():
>    return b
>
>exec "a()" in {'b':1, 'a':a}
>
>b doesn't seem to passed as a global into the function a.
>
This seems possible:
 >>> exec """def a():
 ...    print 'from a()'
 ...    return b
 ... print a()
 ... """ in {'b':1, 'a':a}
 from a()
 1

I'd guess the dictionary for globals used by the function is
determined by the context of the execution of the def?




More information about the Python-list mailing list