Pre-PEP: Dictionary accumulator methods
a.schmolck at gmx.net
Sun Mar 20 02:26:41 CET 2005
"Raymond Hettinger" <vze4rx4y at verizon.net> writes:
> 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)
> In simplest form, those two statements would now be coded more readably as:
> d.appendlist(key, value)
The relatively recent "improvement" of the dict constructor signature
(``dict(foo=bar,...)``) obviously makes it impossible to just extend the
constructor to ``dict(default=...)`` (or anything else for that matter) which
would seem much less ad hoc. But why not use a classmethod (e.g.
Or, for the first and most common case, just a bag type?
More information about the Python-list