[Python-Dev] setdefault's second argument

Wolfgang Lipp paragate at gmx.net
Tue Aug 30 21:45:23 CEST 2005


On Tue, 30 Aug 2005 20:55:45 +0200, Tim Peters <tim.peters at gmail.com>  
wrote:

> [Wolfgang Lipp]
>> reminds me of dict.get()... i think in both cases being explicit::
>>
>>     beast = d.setdefault( 666, None )
>>         ...
>
> Do you actually do this with setdefault()?

well, actually more like::

     def f( x ): return x % 3
     R = {}
     for x in range( 30 ):
         R.setdefault( f( x ), [] ).append( x )

still contrived, but you get the idea. i was really excited when finding  
out
that d.pop, d.get and d.setdefault work in very much the same way in  
respect
to the default argument, and my code has greatly benefitted from that. e.g.

     def f( **Q ):
         myoption = Q.pop( 'myoption', 42 )
         if Q:
             raise TypeError(...)

_w




More information about the Python-Dev mailing list