One function calling another defined in the same file being exec'd

Terry Reedy tjreedy at udel.edu
Fri Jan 8 19:35:39 EST 2010


On 1/8/2010 12:02 PM, Mitchell L Model wrote:


> On further reflection, I will add that
> what appears to be happening is that during import both the global and
> local dictionaries are set to a copy of the globals() from the importing
> scope and that copy becomes the value of the module's __dict__ once
> import has completed successfully.

I have no idea why you think that. The module dict starts empty except 
for __name__, __file__, and perhaps a couple of other 'hidden' items. It 
is not a copy and has nothing to do with importing scopes.

 > and that copy becomes the value of the module's __dict__ once
 > import has completed successfully.

That new dict becomes .... .

> Because exec leaves locals() and globals() distinct,

Not necessarily.

In 3.x, at least,
exec(s)
executes s in the current scope. If this is top level, where locals is 
globals, then same should be true within exec.

d = {}
exec(s, d)

In 3.x, at least, d will also be used as locals.

exec(s, d, d)

Again, globals and locals are not distinct.

It would seem that in 3.x, the only way for exec to have distinct 
globals and locals is to call exec(s) where they are distinct or to pass 
distince globals and locals.

Some of the issues of this thread are discussed in Language Reference 
4.1, Naming and Binding. I suppose it could be clearer that it is, but 
the addition of nonlocal scope complicated things.

Terry Jan Reedy




More information about the Python-list mailing list