Pre-PEP: Dictionary accumulator methods

Raymond Hettinger vze4rx4y at verizon.net
Sat Mar 19 02:54:58 EST 2005


> >  d.count(key, qty)
> >  d.appendlist(key, *values)

[Bengt Richter]
> How about an efficient duck-typing value-incrementer to replace both?

There is some Zen of Python that argues against this interesting idea. Also, I'm
concerned that by folding appendlist() into valadd() we would lose an important
cue that a list is being built-up.

Another issue is that duck-typed multiple-dispatch is only readable when the
type of the input argument is obvious from the surrounding code.  Given
d.valadd(x), it is hard to grok if x was created by some code far away.  Since a
primary goal is readability and clarity, having two separate, concrete methods
is likely better than having a single more-abstracted multi-purpose method.  The
performance gains are just icing on the cake.



> I'm thinking the idea that the counting is happening with the value
corresponding
> to the key should be emphasised more. Hence valadd or such?

How about countkey() or tabulate()?



Raymond Hettinger





More information about the Python-list mailing list