Pre-PEP: Dictionary accumulator methods
Dan Sommers
me at privacy.net
Sat Mar 19 14:00:30 EST 2005
On Sat, 19 Mar 2005 15:17:59 GMT,
"Raymond Hettinger" <vze4rx4y at verizon.net> wrote:
> [Dan Sommers]
>> Curious that in this lengthy discussion, a method name of
>> "accumulate" never came up. I'm not sure how to separate the two
>> cases (accumulating scalars vs. accumulating a list), though.
> Separating the two cases is essential. Also, the wording should
> contain strong cues that remind you of addition and of building a
> list.
Agreed, with a slight hedge towards accumulation or tabulation rather
than addition. I don't think "summation" gets us anywhere, either.
Are the use cases for qty != 1 for weighted averages (that's the only
one I can think of off the top of my head)? Is something like this:
def accumulate( self, key, *values ):
if values == ( ):
values = 1
try:
self[ key ] += values
except KeyError:
if type( key ) == int:
self[ key ] = 1
else
self[ key ] = *values
possible? It's more "klunky" than I thought it would be before I
started typing it out.
Then we'd have these two use cases:
histogram = { }
for word in text.split( ):
histogram.accumulate( word )
and
org_chart = { }
for employee in employees:
org_chart.accumulate( employee.manager, employee.name )
Regards,
Dan
--
Dan Sommers
<http://www.tombstonezero.net/dan/>
μ₀ × ε₀ × c² = 1
More information about the Python-list
mailing list