Pre-PEP: Dictionary accumulator methods

George Sakkis gsakkis at rutgers.edu
Sun Mar 20 01:02:59 EST 2005


"Michael Spencer" <mahs at telcopartners.com> wrote:

> I could imagine a class: accumulator(mapping, default, incremetor) such that:
>   >>> my_tally = accumulator({}, 0, operator.add)
>   or
>   >>> my_dict_of_lists = accumulator({}, [], list.append)
> or
>   >>> my_dict_of_sets = accumulator({}, set(), set.add)
>
> then: .accumulate(key, value) "does the right thing" in each case.
>
> a bit cumbersome, because of having to specify the accumulation method, but
> avoids adding methods to, or sub-classing dict
>
> Michael

That's the equivalent of reduce() for mappings. Given the current trend of moving away from
traditional functional features (lambda,map,filter,etc.), I would guess it's not likely to become
mainstream.

George





More information about the Python-list mailing list