Question about "exec in globals, locals"

Giles Brown giles_brown at hotmail.com
Sat Jul 5 14:41:27 EDT 2003


Adrien Di Mascio <Adrien.DiMascio at logilab.fr> wrote in message news:<mailman.1057316108.30364.python-list at python.org>...
> Hi,
> > source = """
> > class FirstClass:
> >     pass
> > class SecondClass:
> >     References = [FirstClass]
> > """
> 
> 
> When you specify both locals() and globals(), here's what happen :
> 
> """
> class FirstClass:
>     pass
> 
> ## ---------> Here, your locals() dict have been updated, but not your
> ## globals() one.
> ## But, here, you can do : References = [FirstClass] since your
> ## locals()
> ## know about 'FirstClass'
> 
> class SecondClass:
>     ## ---------> Here, your locals() dict is a new one since you've
>     just
>     ## entered a new scope. So, 'FirstClass' is neither defined in
>     ## 'locals()' nor in 'globals()', that's why you have your
>     NameError
> """
> 
> I'm not quite sure of my explanation, but that could explain your
> problem. Sorry if I've made any Python mistake.
Hmmm.  You might be right, but wouldn't you expect the scope of SecondClass to
be nested within the scope of the passed in locals()?

I find this quite confusing, which unusual for a Python ;-)

Thanks,
Giles Brown




More information about the Python-list mailing list