Is this possible: scoping question with exec

Brian Kelley bkelley at
Fri Aug 24 16:41:39 CEST 2001

Bengt Richter wrote:

>On Thu, 23 Aug 2001 15:34:12 -0500, Brian Kelley <bkelley at> 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?
If that's the case though, I would have expected a compiler/syntax 
error.  I was hoping that I could pass globals into a compiled function 
though.  Guess I'll have to look at the source code and see if this is 
not possible.

Brian Kelley
Whitehead Institute for Biomedical Research

More information about the Python-list mailing list