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

Jacob Holm jh at improva.dk
Thu Apr 16 14:20:01 CEST 2009


Hi Arnaud

Arnaud Delobelle wrote:
>
> On 16 Apr 2009, at 11:33, Jacob Holm wrote:
>>
>> [snip my example]
>
> _not_provided = object()
>
> def min(first, *rest, key=_not_provided, default=_not_provided):
> if not rest:
> rest = iter(first)
> for first in rest:
> break
> else:
> if default is _not_provided:
> raise ValueError("min() arg is an empty sequence")
> else:
> return default
> if key is _not_provided:
> for el in rest:
> if el < first:
> first = el
> else:
> fkey = key(first)
> for el in rest:
> elkey = key(el)
> if elkey < fkey:
> first, fkey = el, elkey
> return first
>
>
Yes, that is indeed a bit easier on the eyes, but doesn't work on 2.6. 
Also your version treats the case "min(default=0)" differently from 
mine. That might be a good thing though :) The only reason I could see 
for anyone hitting that case would be a use like "min(*values, 
default=0)", and that is much better handled by dropping the star anyway 
because of the special case for sequences of length 1.

Cheers
- Jacob





More information about the Python-ideas mailing list