[Python-ideas] max() allowed to take empty list

Stephen J. Turnbull stephen at xemacs.org
Tue Apr 13 04:58:03 CEST 2010

Tom Pinckney writes:

 > None makes sense as a return value in many contexts. For example,
 > hypothetical code like:
 > if max(values) >= 90: return True   # works even if values is []

As Chris pointed out, this doesn't work at all in Python 3.  But even
in Python 2, one of "max([]) >= bound" and "min([]) >= bound" must
fail to work correctly in terms of the mathematical limits for max and
min.  ">=" cannot know whether the "None" it sees was derived from
"max([])", and should be interpreted as -infinity, or if it was
derived from "min([])" and should be interpreted as +infinity.  (And
in fact it failed to error in Python 2 only because the comparison was
done on the basis of the address of the object in memory, which is
obviously pretty arbitrary, and in theory could even change from one
invocation of Python to the next.)

Since Python's built-in numbers don't support infinity, there's no way
for those functions to return the appropriate mathematical objects, so
they should error when given an empty sequence.

More information about the Python-ideas mailing list