[Python-3000] A request to keep dict.setdefault() in 3.0

tav tav at espians.com
Mon Jul 9 20:59:11 CEST 2007


> PEP 3100 suggests dict.setdefault() may be removed in Python 3, since
> it is in principle no longer necessary (due to the new defaultdict type).
>
> However, there is another class of use cases which use setdefault for
> its limited atomic properties - the initialization of non-mutated
> data structures that are shared among threads.  (And defaultdict
> cannot achieve the same thing.)

+1

setdefault's ability to return current value is also a very useful
functionality and has saved writing:

  if key not in dict:
    value = <compute-value>
    dict[key] = value

with the simpler:

  value = dict.setdefault(key, <compute-value>)

Is there a better way to do the above without .setdefault?

-- 
love, tav
founder and ceo, esp metanational llp

plex:espians/tav | tav at espians.com | +44 (0) 7809 569 369


More information about the Python-3000 mailing list