[Python-ideas] grouping / dict of lists
ncoghlan at gmail.com
Sat Jun 30 05:01:51 EDT 2018
On 30 June 2018 at 16:25, Guido van Rossum <guido at python.org> wrote:
> On Fri, Jun 29, 2018 at 3:23 PM Michael Selik <mike at selik.org> wrote:
>> I included an alternate solution of a new class, collections.Grouping,
>> which has some advantages. In addition to having less of that "heavy-handed"
>> feel to it, the class can have a few utility methods that help handle more
>> use cases.
> Hm, this actually feels heavier to me. But then again I never liked or
> understood the need for Counter -- I prefer basic data types and helper
> functions over custom abstractions. (Also your description doesn't do it
> justice, you describe a class using a verb phrase, "consume a sequence and
> construct a Mapping". The key to Grouping seems to me that it is a dict
> subclass with a custom constructor. But you don't explain why a subclass is
> needed, and in that sense I like the other approach better.
I'm not sure if the draft was updated since you looked at it, but it
does mention that one benefit of the collections.Grouping approach is
being able to add native support for mapping a callable across every
individual item in the collection (ignoring the group structure), as
well as for applying aggregate functions to reduce the groups to
single values in a standard dict.
Delegating those operations to the container API that way then means
that other libraries can expose classes that implement the grouping
API, but with a completely different backend storage model.
> But I still think it is much better off as a helper function in itertools.
I thought we actually had an open enhancement proposal for adding a
"defaultdict.freeze" operation that switched it over to raising
KeyError the same way a normal dict does, but I can't seem to find it
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Python-ideas