kwarg references

Steve Holden steve at holdenweb.com
Thu Apr 26 07:46:05 EDT 2007


Gabriel Genellina wrote:
> En Tue, 24 Apr 2007 22:31:59 -0300, Calvin Spealman <ironfroggy at gmail.com>  
> escribió:
> 
>> In the internal API when a C function is called and passed a kwarg
>> dictionary, is there any case where anything else has a reference to
>> it? I checked with the following code and it looks like even if you
>> explicitly pass a dictionary with **kwargs, it still copies the
>> dictionary anyway.
> 
> See the Python Reference Manual, 5.3.4 Calls
> http://docs.python.org/ref/calls.html
> 
> Unfortunately it does not explicitely *guarantee* it will be a new  
> dictionary. At least the way I read the docs, on a function call like this:
> 
> def foo(**kwargs): pass
> d = {'a':1, 'b':2)
> foo(**d)
> 
> Python could bind the existing d object to the formal parameter kwargs and  
> still comply with the documented behavior. Some of my own code would break  
> if that happened... :(
> 
The point is, of course, that inside the function you don't know whether 
extra keyword arguments were provided as a part of a call with a 
**kwargs argument or individually provided as kw1=value1. In the latter 
case, obviously, there is no dict to choose whether to copy or not, so 
the interpreter has no choice but to create a new dictionary.

On the one hand it would seem sensible to extend the definition of the 
semantics so it is clear beyond doubt that mutating a **kwargs parameter 
dictionary inside a function body does not change any dictionary that 
was provided as a **argument, /even in the case where there is a 
one-to-one correspondence between them/.

On the other hand it would also seem sensible to realise that this 
latter case is so unusual that providing explicit semantics to support 
it would take much more implementation work than always making a copy of 
any **kw argument.

On the third hand, even *I* seem sensible sometimes so you just never 
can tell :-)

regards
  Steve
-- 
Steve Holden       +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb     http://del.icio.us/steve.holden
Recent Ramblings       http://holdenweb.blogspot.com




More information about the Python-list mailing list