stupid namespace tricks

Alex Martelli aleaxit at yahoo.com
Tue Sep 14 10:43:29 EDT 2004


David Rysdam <drysdam at ll.mit.edu> wrote:
   ...
> 2) function_defaults.py imports nothing, defines logError() and also 
> sets up a default global like so:
> 
> dictGlobal = {'logError':logError}

the function logError's globals are therefore the globals of module
function_defaults.

> 3) And then in myDaemon I "import functions_default" and do this:
> 
> exec(body, functions_default.dictGlobal)

function_defaults.dictGlobal [[I assume the missing trailing s was just
a typo]] is the globals for that body, but not of course for any
functions which that body might call from other modules (such as
logError), which will each use the globals of the respective module.

> I added this to myDaemon.py ahead of the exec():
> 
> functions_default.dictGlobal['id'] = scriptlet['id']
> 
> But when I get to logError, 'id' isn't there.

Right, though if would be if you had coded:

vars(function_defaults)['id'] = scriptlet['id']

(again I assume there are typos regarding the 's' location...).


Alex



More information about the Python-list mailing list