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

Jacob Holm jh at improva.dk
Wed Apr 15 19:31:35 CEST 2009


Adam Atlas wrote:
> 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.

+1, I have often wanted that.

Cheers
- Jacob



More information about the Python-ideas mailing list