On Tue, Jul 3, 2018 at 8:33 AM, Steven D'Aprano <steve@pearwood.info> wrote:
but why are we using key values by hand when grouping ought to do it for 
us, as Michael Selik's version does?

    grouping(words, key=len)

because supplying a key function is sometimes cleaner, and sometimes uglier than building up a comprehension -- which I think comes down to:

1) taste (style?)

2) whether the key function is as simple as the expression

3) whether you ned to transform the value in any way.

This argument is pretty much the same as whether you should use a comprehension or map:

map(len, words)


(len(word) for word in words)

In that case, map() looks cleaner and easier, but when you have something less simple:

map(operator.attrgetter('something'), some_objects)


(object.something for object in some_objects)

I like the comprehension better.

add a filter, and comps really get nicer -- after all they were added to the language for a reason.

Then when you add the additional complication of needing to "transform" the value as well, it's easy to do with the comprehension, but there is no way to do it with only a key function.

I think the "confilct" here is that Micheal started with  a bunch of examples that area ll well suited to the key_function approach, and Nicolas started with a use-case that is better suited to the comprehension / (key,value) approach.

However, while the key, value approach can be reasonably (if a bit klunky) used everywhere the key function approach can, the opposite is not true (for when the value needs to be transformed as well.

But in the spirit of "Python has both map and comprehensions", I say let's use both!

* The default behavior is to process a (key.value) pair.

* A key function can be provided in which case it is used, and the value is the full item.

* A value function can be provided, in which case, it is used to "process" the value. 

If this is too confusing an interface, we could forget the value function, and folks would have to use the (key, value) interface if they need to transform the value.

What makes no sense to me is having the identify function as the default key (and yes, it is the identity function, it would return the actual object, or not be there at all) -- the grouping would be done by the hash of key after passing through the key function). 

That's because having a default that is (almost) completely useless  makes no sense -- it might as well be a required parameter.

(unless there was a value function as well, in which case, it's not a completely useless default).



Christopher Barker, Ph.D.

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception