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

Nick Coghlan ncoghlan at gmail.com
Tue Apr 13 14:37:15 CEST 2010


Stephen J. Turnbull wrote:
> 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.

Small correction there: "not all of Python's built-in numbers".

The bigger problem is that when using max() and min() the *type* of
value you're dealing with will usually matter. Returning None is almost
certainly wrong, but the functions otherwise have no idea what to return
if an empty sequence is supplied, since they don't know what the program
expected it to be a list *of* if it contains no values.

In line with what Mark posted, the API extension that probably makes the
most sense is to allow a bound to be specified in the call. For example:

x = min(seq, upper_bound=0)
x = min(seq, upper_bound=float('inf'))
x = max(seq, lower_bound=0)
x = max(seq, lower_bound=float('-inf'))

where the stated bound is included in the values to be compared.

This has some minor advantages over the alternatives, but would need
some genuine real world use cases to justify complicating the core API.
It also runs afoul of the language moratorium, and hence is off the
table for the immediate future.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------



More information about the Python-ideas mailing list