[Python-ideas] Changing all() and any() to take multiple arguments

Andrew Barnert abarnert at yahoo.com
Mon Aug 26 09:05:04 CEST 2013


From: Clay Sweetser <clay.sweetser at gmail.com>
Sent: Sunday, August 25, 2013 9:50 PM


>I believe that the all() and any() library functions should be modified to accept multiple arguments as well as single iterators.

But that would make a number of expressions ambiguous. Is all([0, 0, 0]) true because it has one true argument, or false because it has three false arguments? What about all([0])? Or all([]), or all()? Even if you invented rules for which interpretation wins in these cases, it would still be ambiguous to human readers. Especially in cases where (as usual) the arguments aren't actually a static list display, but a variable, comprehension, or other expression.

There are a few places in Python where we have such ambiguity, such as giving the string % operator a single value, but I don't think most people think that's a good thing. In fact, a few such cases were eliminated in Python 3.0, and newer functions like str.format or itertools.chain all avoid it.

>Currently, code such as this (taken from a sublime text plugin), breaks:
>    def is_enabled(self):
>        return all(
>            self.connected,
>            not self.broadcasting,
>            self.mode is 'client'
>        ) 
>
>
>Meaning either that one must write either this, and use parenthesis to avoid some obscure order of operations error:
>    def is_enabled(self):
>        return (
>            (self.connected) and
>            (not self.broadcasting) and
>            (self.mode is 'client')
>        ) 


Do you really think anyone doesn't know that the dot in "self.connected" binds more tightly than "and"? The fact that you can add excessive parentheses doesn't that mean you should, or that most people do.

And really, I think this is more readable than the way you'd like to write it. For a short, static sequence, "all" is just extra verbiage getting in the way—exactly as in English, where you wouldn't say "All of John, Mary, and Pete went to the ceremony."


More information about the Python-ideas mailing list