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

Arnaud Delobelle arnodel at googlemail.com
Thu Apr 16 11:10:52 CEST 2009


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:

 >>> class Err(Exception): pass
...
 >>> def it():
...     raise Err()
...     yield 42
...
 >>> min(it())
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "<stdin>", line 2, in it
__main__.Err
 >>> min2(it())
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "<stdin>", line 5, in min2
   File "<stdin>", line 2, in it
__main__.Err
 >>> min2(it(), default=12)
12

Shouldn't the last one raise Err as well?

-- 
Arnaud




More information about the Python-ideas mailing list