[Python-ideas] Making min and max behave more like any and all

Guido van Rossum guido at python.org
Wed Nov 30 19:15:14 CET 2011


On Wed, Nov 30, 2011 at 9:30 AM, Jakob Bowyer <jkbbwr at gmail.com> wrote:

> I just had something pointed out to me in #python that min and max accept
> *args, I know for a fact that any and all only use a single iterable
> password.
> Shouldn't we allow either one of the two ideas?
>
> Either max/min take only iterable arguments.
> OR
> Allow any/all to use *args
>

This seems consistency for consistency's sake, not for any particularly
good reason. min/max are quite different from any/all, e.g. min() or max()
without arguments makes no sense, while any() / all() on an empty list has
a perfectly fine meaning (False / True, respectively).

In the second case this becomes legal
>
> any(1 in a, 2 in b, 3 in c)
>

But that's just alternative syntax for

  1 in a or 2 in b or 3 in c

except that it doesn't have shortcut semantics, so it would be an
attractive nuisance. Note that any() / all() *do* have shortcut semantics,
in the sense that they stop iterating over the argument iterable as soon as
they have an answer that forces the outcome (True in the case of any(),
False in the case of all()). This doesn't apply to min() or max() -- you
always have to look at all values before you can decide on which is the
largest. (It would be different if we had a general infinity, but we don't
-- only floating point numbers support infinity).


> In the first case this becomes illegal
> max(1,2,3,4,5)
>

Which I presume is rarely needed but if necessary can easily be written as
max((1, 2, 3, 4, 5)).

-- 
--Guido van Rossum (python.org/~guido <http://python.org/%7Eguido>)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20111130/25de239e/attachment.html>


More information about the Python-ideas mailing list