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

Jared Grubb jared.grubb at gmail.com
Thu Apr 16 20:19:32 CEST 2009

On 16 Apr 2009, at 02:10, Arnaud Delobelle wrote:
> On 16 Apr 2009, at 04:39, Jared Grubb wrote:
>> def min2(*vars, **kw):
>>    try:
>>        if 'key' in kw:
>>             return min(*vars, key=kw['key'])
>>        return min(*vars)
>>    except Exception:
>>        if 'default' in kw:
>>            return kw['default']
>>        raise
> Nitpick: [...]

Yes, the "except Exception" was intentional such that "default=..."  
gives a no-throw guarantee. (I originally had "except TypeError", but  
then that would swallow all TypeError, even those given by the  
iterator; and between THOSE two behaviors, no-throw seemed most  

I'm 0 on whether that should be the semantics or not, but as some have  
pointed out, swallowing bugs in iterators is not always a good thing.  
On the other hand, a no-throw min is kinda nice too...

I personally prefer some of the other posters' versions that basically  
do "default catches only the TypeError that min would have thrown if  
iterator was empty" semantics.


More information about the Python-ideas mailing list