This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: any, all and sum should accept variadic args
Type: behavior Stage: resolved
Components: Versions: Python 3.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: sedrubal, serhiy.storchaka, steven.daprano
Priority: normal Keywords:

Created on 2017-02-03 11:43 by sedrubal, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (5)
msg286859 - (view) Author: (sedrubal) Date: 2017-02-03 11:43
any, all and sum (and maybe some other functions) should accept variadic args. It should be possible to do things like this:

>>> any(True, False, True)
True
>>> all(True, False, True)
False
>>> sum(1, 2, 3)
6

This was compliant to max and min behaviour:

>>> max(1, 2, 3)
3
>>> min(1, 2, 3)
1
msg286860 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-02-03 12:04
There is no such need. You can use operators.

any(arg1, arg2, arg3) -> arg1 or arg2 or arg3
all(arg1, arg2, arg3) -> arg1 and arg2 and arg3
sum(arg1, arg2, arg3) -> arg1 + arg2 + arg3
msg286864 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2017-02-03 13:38
Serhiy, that doesn't generalise to code like:

    any(a, b, c, *extras)

which is hard to write out by hand. You would have to say 

    bool(a or b or c or any(extras))

I think this might be worth considering on Python-Ideas. It will probably be rejected, but if Sedrubal cares enough to raise the idea, it could be discussed.

However, sum should be taken off the list. The problem with accepting variadic args is that it clashes with current behaviour:

    sum( [[1], [2], [3]], [4, 5] )

That would be ambiguous. Under the suggested variadic form, that should return a list:

    [[1], [2], [3], 4, 5]

but it already has a meaning in Python today:

    [4, 5, 1, 2, 3]


So sum() with variadic args would be ambiguous, or would break backwards compatibility. Neither of those would be acceptable.
msg286874 - (view) Author: (sedrubal) Date: 2017-02-03 15:56
Thanks for your answers and for showing the issue with sum.

I think this would make python just a bit more sexier as it already is ;) Are there any other disadvantages (performance, ...)?

Do you think I should send a mail to the ideas list?
msg286876 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2017-02-03 16:10
> Do you think I should send a mail to the ideas list?

Personally, I don't think so. You want to write any(a, b, c, d), 
but you can get the same effect now by writing any([a, b, c, d]).
There is unlikely to be any significant performance difference.
History
Date User Action Args
2022-04-11 14:58:42adminsetgithub: 73619
2017-02-03 16:10:32steven.dapranosetmessages: + msg286876
2017-02-03 15:56:48sedrubalsetmessages: + msg286874
2017-02-03 13:38:54steven.dapranosetnosy: + steven.daprano
messages: + msg286864
2017-02-03 12:04:51serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg286860

resolution: rejected
stage: resolved
2017-02-03 11:43:19sedrubalcreate