funcs vs vars in global namespace

David Rysdam drysdam at ll.mit.edu
Wed Sep 15 13:57:20 CEST 2004


Bengt Richter wrote:
> On Tue, 14 Sep 2004 14:25:04 -0400, David Rysdam <drysdam at ll.mit.edu> wrote:
> 
> 
>>Alex Martelli wrote:
>>
>>>David Rysdam <drysdam at ll.mit.edu> wrote:
>>>   ...
>>>
>>>
>>>>OK, dumb question #1:
>>>>
>>>>Why do this:
>>>>
>>>>sub_module = __import__(which_module_this_time)
>>>>vars(sub_module).update(which_dict_this_time)
>>>>
>>>>When I could just do this:
>>>>
>>>>__import__(which_module_this_time, which_dict_this_time)
>>>>
>>>>?
>>>
>>>
>>>You can do whatever you wish, but what makes you think these constructs
>>>have similar effects?  Quoting from Python's online docs on __import__,
>>>
>>>"""
>>> the standard implementation  does not use its locals argument at all,
>>>and uses its  globals only to determine the package context of the
>>>import statement
>>>"""
>>>
>>>In short, the standard implementation of __import__ does NOT alter in
>>>any way the dict of the module it imports.
>>>
>>>
>>>Alex
>>
>>Ah, I see.
>>
>>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 is getting too non-obvious, magical and unmaintainable, though.
> 
> 
> Maybe this will give you some ideas:
> 

Aha, yes indeed this gives me ideas.  Very excellent tutorial, I've 
saved it for when I manage to confuse myself again later.  I think I 
know just how to do what I want to do now, though I'm beginning to 
question the wisdom of providing the feature in question at all.  But if 
I'm going to, it should be right, so I still thank you.



More information about the Python-list mailing list