Is this possible: scoping question with exec

Emile van Sebille emile at fenx.com
Fri Aug 24 16:12:57 CEST 2001


 exec(a.func_code, {'b':4})

--

Emile van Sebille
emile at fenx.com

---------
"Brian Kelley" <bkelley at wi.mit.edu> wrote in message
news:3B8667A3.7020806 at wi.mit.edu...
> Bengt Richter wrote:
>
> >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?
> >
> 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