[Python-ideas] Make max() stable
Andrew Barnert
abarnert at yahoo.com
Sat Jan 18 06:42:13 CET 2014
From: Terry Reedy <tjreedy at udel.edu>
Sent: Friday, January 17, 2014 4:48 PM
> On 1/17/2014 7:09 PM, Andrew Barnert wrote:
>> On Jan 17, 2014, at 15:34, Terry Reedy <tjreedy at udel.edu> wrote:
>
>>> Min and max are inherently functions of multisets, with order
>>> irrelevant but duplicate values allowed.
>
> I should have said a multiset of comparable objects.
>
>> I'm not sure that's necessarily true. The maximal value of a
> sequence
>> makes every bit as much sense as the maximal value of a set or
>> multiset
>
> A list of comparable objects *is* a multiset of comparable objects.
No, a list is a multiset _with order_. Which is the whole point. You claimed that because it's a multiset, the order doesn't matter. But because the domain of max is a sequence (or, better, as you correctly point out, an iterable), not a multiset, the order does matter. Otherwise this entire question wouldn't arise in the first place.
>> … asking for max(experiments, key=itemgetter(1)) is a meaningful thing to do.
>
> max((value,time) for time,value in experiments)
>
> gives the lastest high value. In general
>
> max((val,i) for i,val in enumerate(iterable))
>
> does the same.
Sure, given my list of experiments in time order, these give the same result as my expression (except with the members of the tuple reversed, which we can ignore). And?
No matter how you write this, you're not just picking the highest value, you're picking the highest value _with the earliest time_. Which is a meaningful thing to do.
> If max gave the last maximum, it would be trickier to get the first maximum,
> just as it is now to get the last minimum.
Of course. If you read my whole message, that was exactly my point: both are useful. Well, that, and the fact that the current behavior is (a) useful more often than the opposite, and (b): compatible with reams of existing code. And therefore, it would be a bad idea to gratuitously change max to return the last instead of the first. Which I think you agree with completely, so I'm not sure why you're trying to disprove it.
More information about the Python-ideas
mailing list