[Python-ideas] 'default' keyword argument for max(), min()
Raymond Hettinger
python at rcn.com
Wed Apr 15 20:17:07 CEST 2009
[Adam Atlas]
>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.
Could you write your proposal out in pure python so
we can see how it interacts with the key-keyword
argument and how it works when the number of
positional arguments is not one.
Will min(default=0) still return a TypeError?
Will min(1, 2, default=0) return 0 or 1?
Will min([1,2], default=0) return 1? # different from min([0,1,2])
Also, can you post some snippets of real-world use cases.
Is the default value always zero (even for max)?
I'm wondering if there are any patterns to the use cases.
I don't doubt that the use cases exist, I'm just curious
what they are and what it says about how min() and max()
are being used.
Are the typical use cases occuring with iterables that are also
sequences? If so, why would a default argument be better
than a conditional expression:
x = min(s) if s else 0
Raymond
More information about the Python-ideas
mailing list