[Python-Dev] arguments policy: **kwargs.pop()

Christian Tismer tismer at stackless.com
Fri Apr 11 19:30:52 CEST 2014


Thank you too, Tres.
Somehow I had a brain shortcut and forgot that
the dict is locally generated, *because* of the stars.

Good to become adjusted and restarted, sorry about the noise.

ciao - Chris


On 11/04/14 05:48, Tres Seaver wrote:
> On 04/10/2014 10:12 PM, Christian Tismer wrote:
> 
>> I always used the policy that arguments are never changed by a 
>> function, unless explicitly stated. But since I see this pattern 
>> quite frequently, I wanted to ask if I am right by thinking this 
>> way, or if the general policy is more like "arguments may be 
>> destroyed by default".
> 
>> What do you think? Is this bad style and should be noticed 
>> somewhere, or is the caller supposed to protect the arguments,
>> or are my worries useless?
> 
> The caller can't know or care that the function / method pops 
> arguments::
> 
> $ python Python 2.7.3 (default, Feb 27 2014, 19:58:35) [GCC 4.6.3] 
> on linux2 Type "help", "copyright", "credits" or "license" for
> more information.
>>>> def foo(**kw):
> ...     bar = kw.pop('bar', 'BAR') ...     print 'bar: %s' % bar 
> ...     print 'kw: %s' % kw ...
>>>> foo()
> bar: BAR kw: {}
>>>> foo(bar='baz')
> bar: baz kw: {}
>>>> foo(bar='baz', bam='qux')
> bar: baz kw: {'bam': 'qux'}
>>>> mykw = {'bar': 'baz', 'bam': 'qux'} foo(**mykw)
> bar: baz kw: {'bam': 'qux'}
>>>> mykw
> {'bam': 'qux', 'bar': 'baz'}
> 
> because the caller gets its own copy of 'kw', even when called
> with an existing dict.
> 
> 
> Tres.
> 
> _______________________________________________ Python-Dev mailing 
> list Python-Dev at python.org 
> https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: 
> https://mail.python.org/mailman/options/python-dev/tismer%40stackless.com
>
>
> 
-- 
Christian Tismer             :^)   <mailto:tismer at stackless.com>
Software Consulting          :     Have a break! Take a ride on Python's
Karl-Liebknecht-Str. 121     :    *Starship* http://starship.python.net/
14482 Potsdam                :     PGP key -> http://pgp.uni-mainz.de
phone +49 173 24 18 776  fax +49 (30) 700143-0023
PGP 0x57F3BF04       9064 F4E1 D754 C2FF 1619  305B C09C 5A3B 57F3 BF04
      whom do you want to sponsor today?   http://www.stackless.com/


More information about the Python-Dev mailing list