changing local dictionnary for functions

Boris Boutillier boris at cantal.lip6.fr
Tue Apr 9 08:08:12 EDT 2002


On Tue, 09 Apr 2002 12:04:44 +0200, Alex Martelli wrote:

> Steve Holden wrote:
> 
>> "Boris Boutillier" <boris at cantal.lip6.fr> wrote in message
>> news:pan.2002.04.08.18.15.31.115122.19669 at cantal.lip6.fr...
>>> Is there a way to pass a local dictionnary to a function as it is
>>> possible to do with a file with the execfile statement ?
>>>
>>> In the python reference manual, in the section 4.1 : Code
>>> Blocks,execution frames and namespace, there is a table that say that
>>> function body uses globals namespace of the containing block and a new
>>> locals namespace. And there is a note that say global and local can be
>>> overrident, how is it possible ? I think it would be the answer to my
>>> first question.
>> 
>> I suspect this is an arror in the documentation, since the namespaces
>> provided to a function call are (unless I am mistaken) determined
>> solely by the context of the call. Sorry.
>> 
>> If  I'm wrong, you can guarantee that three different people will point
>> this out within three minutes of my hitting the "send" key :-)
> 
> The only way that occurs to _me_ for "changing a function's
> dictionaries" would be to extract the code object from the function and
> build a new function (with module new) using that code object and
> whatever dictionary you want for globals.  Even so, the locals (not
> really a dictionary... they only halfway-masquerade as one through the
> built-in function of that name) would be created anew at each call to
> the functions, so it still wouldn't meet the request by Boris as I
> understand it.
> 
> 
> Alex
> 

Thanks all for you answer, effectively alex this does not meet exactly my
request but I'm looking in this direction, by doing some code
manipulation I'll suppress use of local variables by replacing load_fast
by load_global opcodes etc... Don't where this leads but I'll continue to
dig !

Boris



More information about the Python-list mailing list