funcs vs vars in global namespace

Jeff Shannon jeff at ccvcorp.com
Tue Sep 14 15:45:27 EDT 2004


David Rysdam wrote:

> Alex Martelli wrote:
>
>>> sub_module = __import__(which_module_this_time)
>>> vars(sub_module).update(which_dict_this_time)
>>
>
> Your "which_dict_this_time" dictionary, how are you imagining that 
> working?  I was just mapping function name strings to functions 
> ({'logError':logError}), but (long story short) that isn't working how 
> I want.  But shouldn't I be able to define the function right there in 
> the dictionary itself?


Perhaps this would be a bit clearer with more-meaningful names.  (Of 
course, that's presuming that I'm reading Alex's intent correctly... :) )

std_global_dict = { 'logError': logError, ... }
script_module = __import__('some_script_module')
vars(script_module).update(std_global_dict)

This will have the effect of injecting all of the specified names (in 
std_global_dict) into the scriptlet's module, where they can be used as 
global variables.  (But note that logError(), and other functions put 
into std_global_dict, will execute in the context in which they were 
defined -- that is, logError is using its own module's globals, not 
script_module's globals or std_global_dict.)

Jeff Shannon
Technician/Programmer
Credit International




More information about the Python-list mailing list