[Python-Dev] setdefault's second argument

Tim Peters tim.peters at gmail.com
Tue Aug 30 20:55:45 CEST 2005

[Wolfgang Lipp]
> reminds me of dict.get()... i think in both cases being explicit::
>     beast = d.setdefault( 666, None )
>     beast = d.get( 666, None )
> just reads better, allthemore since at least in my code what comes
> next is invariably a test 'if beast is None:...'. so
>     beast = d.setdefault( 666 )
>     if beast is None:
>         ...

Do you actually do this with setdefault()?  It's not at all the same
as the get() example next, because d.setdefault(666) may _also_ have
the side effect of permanently adding a 666->None mapping to d. 
d.get(...) never mutates d.

> and
>     beast = d.get( 666 )
>     if beast is None:
>         ...
> a shorter but a tad too implicit for my feeling.

Nevertheless, 1-argument get() is used a lot.  Outside the test suite,
I've only found one use of 1-argument setdefault() so far, and it was
a poor use (used two lines of code to emulate what dict.pop() does

More information about the Python-Dev mailing list