Pre-PEP: Dictionary accumulator methods

Duncan Booth duncan.booth at invalid.invalid
Sun Mar 20 10:52:44 CET 2005


Raymond Hettinger wrote:

> The rationale is to replace the awkward and slow existing idioms for
> dictionary based accumulation:
> 
>     d[key] = d.get(key, 0) + qty
>     d.setdefault(key, []).extend(values)
> 

How about the alternative approach of allowing the user to override the 
action to be taken when accessing a non-existent key?

   d.defaultValue(0)

and the accumulation becomes:

   d[key] += 1

and:

   d.defaultValue(function=list)

would allow a safe:

  d[key].extend(values)




More information about the Python-list mailing list