[Python-ideas] 'default' keyword argument for max(), min()

Adam Atlas adam at atlas.st
Wed Apr 15 19:20:15 CEST 2009


I propose adding a "default" keyword argument to max() and min(),  
which provides a value to return in the event that an empty iterable  
is passed. (If no "default" argument is provided, and the iterable is  
empty, it would raise ValueError as it does currently.) I find this to  
be a very common need when using those functions. Of course this is  
already possible with a bit more code, but it depends on what type of  
object the iterable is -- if it supports __len__ or __nonzero__, that  
can be used to check if it's empty beforehand, but if it is a  
generator, for instance, it would have to be converted to a list  
first, which might be undesirable if there is the possibility that it  
is a very large sequence. Adding a "default" keyword argument to max()  
and min() would be an elegant way to centralize and simplify this  
common and useful behaviour.

If there is support for this idea, I can submit a patch implementing it.



More information about the Python-ideas mailing list