exec src in {}, {} strangeness

Greg Ewing greg at cosc.canterbury.ac.nz
Mon Mar 21 20:47:33 EST 2005


Stefan Seefeld wrote:
> Bernhard Herzog wrote:
>
>> When "foo = Foo" is executed, Foo is first looked up in that new locals
>> dictionary.  That fails, so it's also looked up in the globals
>> dictionary a.  That fails as well because Foo was bound in b.  The final
>> lookup in the builtins also fails, and thus you get an exception.

Yes, from the experiment I just did, that does seem to
be what is happening.

> Thanks for the explanation ! I'm still unable to make a conclusion:
> What is wrong ? Am I doing something stupid...? Or is that really a bug?

It seems to be a hangover from the old two-scope system
where local scopes didn't nest. It probably qualifies as
a bug, since it differs from the modern behaviour of
a class definition inside the local scope of a function.

However, if your intention is to define the classes in
the global dict then yes, you are doing something wrong --
you should be passing the same dictionary for both scopes:

   g = {}
   exec stuff_to_define in g, g
   # definitions are now in g

-- 
Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg



More information about the Python-list mailing list