[Python-ideas] Make max() stable

Chris Angelico rosuav at gmail.com
Fri Jan 17 17:15:50 CET 2014


On Sat, Jan 18, 2014 at 3:06 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> Using your weird key function here is going to give bizarre results.
> Consider:
>
> ab = (2, 1)
> min(ab, key=key), max(ab, key=key)
>
> Current behaviour is to return (2, 2). I don't think that returning 2
> for the minimum and 1 for the maximum is more sensible, but that's
> because the key function is not sensible, not because of any objection
> to making max stable in this sense.

Imagine implementing min and max this way (ignoring key= and the
possibility of a single iterable arg):

def min(*args):
    return sorted(args)[0]

def max(*args):
    return sorted(args)[-1]

By that definition, a stable sort means that:

lst = sorted((x,y))
assert lst == [min(lst), max(lst)]

will pass for any x and y.

That said, I don't see any particular use cases for this identity.
Maybe the OP can enlighten?

ChrisA


More information about the Python-ideas mailing list