Pre-PEP: Dictionary accumulator methods

Raymond Hettinger vze4rx4y at verizon.net
Sat Mar 19 17:15:07 EST 2005


[Bengt Richter]
> IMO Raymond's Zen concerns
> are the ones to think about first, and then efficiency, which was one of the
motivators
> in the first place ;-)

Well said.

I find the disassembly (presented in the first post) to be telling.  The
compiler has done a great job and there is no fluff -- all of those steps have
been specified by the programmer and he/she must at some level be aware of every
one of them (pre-instantiation, multiple method lookups and calls, multiple
dictionary accesses, etc).  That is too bad because the intent could have been
stated atomically: d.appendlist(k, v).  Instead, the current idiom turns you
away from what you want done and focuses the attention on how it is done:
d.setdefault(k, []).append(v).  That is too many steps for what should be an
atomic operation (in the eyes of the programmer and code readers).

Likewise, d.tally(k) is as atomic as this expression can get.  Any other steps,
added verbiage, new types, extra arguments, or whatnot are an unnecessary waste
of brain cells.


Raymond Hettinger





More information about the Python-list mailing list