[Python-ideas] 'default' keyword argument for max(), min()
George Sakkis
george.sakkis at gmail.com
Thu Apr 16 23:06:17 CEST 2009
On Thu, Apr 16, 2009 at 2:57 PM, Raymond Hettinger <python at rcn.com> wrote:
> I think the default arg is a bridge too far. This is evidenced by the
> complexity of the suggested implementations (remember the zen of python).
FWIW, the following works good enough for my use cases:
def min2(*args, **kwds):
if 'default' not in kwds:
return min(*args, **kwds)
default = kwds.pop('default')
try: return min(*args, **kwds)
except ValueError, ex:
if 'arg is an empty sequence' in ex.message:
return default
raise
As an aside, it would be nice If min/max start raising a more narrow
ValueError subtype, say EmptyIterableError, so that hacks such as
checking the exception message are not necessary.
George
More information about the Python-ideas
mailing list