[Python-Dev] Behaviour of max() and min() with equal keys
Matthew Woodcraft
matthew at woodcraft.me.uk
Tue Sep 7 23:07:35 CEST 2010
Mark Dickinson wrote:
> Matthew Woodcraft wrote:
>> In CPython, the builtin max() and min() have the property that if there
>> are items with equal keys, the first item is returned. From a quick look
>> at their source, I think this is true for Jython and IronPython too.
> It's actually not clear to me that this behaviour is ideal; it might
> make more sense to have max() return the first item among equal
> largest elements, and min() return the last item.
I don't care a great deal what the behaviour is; I would like it to be
consistent across Python versions, and I think the pragmatic way to
achieve that is to document the current behaviour.
> Can you give examples of code that relies on max and min returning the
> first among equals?
An existing regression test whose output depends on which choice is
made.
(I was writing some code today which had to duplicate the behaviour of a
non-Python program which uses first-among-equals, which is what brought
this question up. In that case I wouldn't think it unreasonable to have
to hand-code the loop rather than using max(), but if in practice Python
is always going to be first-among-equals, it seems to me we might as
well be 'allowed' to take advantage of it.)
-M-
More information about the Python-Dev
mailing list