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

George Sakkis george.sakkis at gmail.com
Wed Apr 15 19:39:04 CEST 2009


On Wed, Apr 15, 2009 at 1:31 PM, Jacob Holm <jh at improva.dk> wrote:
> 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.

Seconded; I've been bitten more than once by this.

George



More information about the Python-ideas mailing list