[Python-ideas] 'default' keyword argument for max(), min()
Steven D'Aprano
steve at pearwood.info
Wed Apr 15 21:11:28 CEST 2009
On Thu, 16 Apr 2009 04:17:07 am Raymond Hettinger wrote:
> [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])
I would expect the answers should be:
Yes, 1 and 1
but I'd be prepared to be talked into:
No, 1 and 1.
> Also, can you post some snippets of real-world use cases.
> Is the default value always zero (even for max)?
I don't believe there should be a default value for default. If you
don't provide an explicit default, the current behaviour should remain
unchanged.
> 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
If s could be either an iterable or a sequence, you would need to write
that as:
s = list(s)
x = min(s) if s else 0
which turns a single conceptual operation into two operations.
--
Steven D'Aprano
More information about the Python-ideas
mailing list