[Python-ideas] "any" and "all" support multiple arguments
Terry Reedy
tjreedy at udel.edu
Tue Aug 1 16:51:41 EDT 2017
On 8/1/2017 9:01 AM, Louie Lu wrote:
> Hi all,
>
> In "min" and "max" builtin-func, it support two style of args:
>
> min(...)
> min(iterable, *[, default=obj, key=func]) -> value
> min(arg1, arg2, *args, *[, key=func]) -> value
To me, two APIs is a nuisance. For one thing, default has to be keyword
only and not just optional. Compare with sum:
>>> sum((2,3,4),5)
14
>>> min((2,3,4),5) # Py3
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
min((2,3,4),5)
TypeError: '<' not supported between instances of 'int' and 'tuple'
>>> min((2,3,4),5) # Py2
5
>>> min(5, (2,3,4))
5
I believe that a version of the second was in original Python (and at
least in 1.3) whereas the first was added later, likely with the new
iterator protocol (2.2). In any case, with *unpacking in displays, the
second is no longer needed.
>>> min(4,3, *[1,2])
1
>>> min((4,3, *[1,2]))
1
If I am correct, perhaps the doc for max and min in
https://docs.python.org/3/library/functions.html#max
should mention that the 2nd is derived from the original syntax, kept
for back compatibility (rather than a new innovation, to be imitated).
I would rather get rid of the exceptional case than emulate it.
> But for "any" and "all", it only support iterable:
>
> all(iterable, /)
> Return True if bool(x) is True for all values x in the iterable.
As Nick pointed out, this is standard now.
>>> list((1,2,3))
[1, 2, 3]
>>> list(1,2,3)
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
list(1,2,3)
TypeError: list() takes at most 1 argument (3 given)
--
Terry Jan Reedy
More information about the Python-ideas
mailing list