4

I was wondering if it was discussed - when designing Python - whether all and any should be implemented as a chain of and and ors respectively? And what was the rationale not to do so.

Right now all and any are implemented equivalently to:

def all(iterable):
    for element in iterable:
        if not element:
            return False
    return True

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False

I was wondering why it not implemented like:

def all(iterable): # alternative idea
    result = True
    for element in iterable:
        if not element:
            return element
        else:
            result = element
    return result

def any(iterable): # alternative idea
    result = False
    for element in iterable:
        if element:
            return element
        else:
            result = element
    return result

This would imply that if you feed all something like all([a,b,c,d,e]) it is equivalent to:

(True and) a and b and c and d and e

and if you feed the same list to any([a,b,c,d,e]), the result would be equal to:

(False or) a or b or c or d or e

I placed (True and) and (False or) between brackets because these are "implicit" elements in the chain.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • Did you see [this question](http://stackoverflow.com/questions/10180344/why-does-python-any-return-a-bool-instead-of-the-value)? I'm tempted to flag as a dupe but maybe there's something you're not satisfied with there. – miradulo Jan 23 '17 at 02:39
  • @Mitch: I honestly did search for duplicates, but failed to find this one. I think you can close this question. Thanks! – Willem Van Onsem Jan 23 '17 at 02:41
  • Your `all()` definition alone would give programmers a misleading result as it would be returning the first instance in the iterable that is false. You no longer hold the single responsibility SOLID principle in that function. – the_constant Jan 23 '17 at 02:41
  • @WillemVanOnsem no worries! – miradulo Jan 23 '17 at 02:42
  • @Vincenzzzochi: well `and` and `or` chains do that as well. So they don't respect the SOLID principle either? – Willem Van Onsem Jan 23 '17 at 02:44
  • @WillemVanOnsem they're not classes, so SOLID does not apply. – the_constant Jan 23 '17 at 02:45
  • @Vincenzzzochi: well for starters, `all` and `any` are functions, not classes. And personally I would see `and` and `or` as functions that happen to have infix syntactical sugar. In languages like Prolog and Haskell `a and b` would be the same as `and(a,b)`. – Willem Van Onsem Jan 23 '17 at 02:47
  • @WillemVanOnsem functions are objects in Python, which have a class of ``. Regardless, even the linked answer coincides with my very first sentence in the first comment I posted. – the_constant Jan 23 '17 at 02:49
  • @Vincenzzzochi: yeah I know that. That's indeed the design principle of probably every dynamic language. I'm only saying that in my experience with functional and logic programming, and operator theory there is nothing special about `and` and `or` either (only in some languages). One might have implemented `and` and `or` as functions as well. For instance in Haskell `(&&) :: Bool -> Bool -> Bool` is simply a function (well everything in Haskell is a function actually that takes exactly one argument). – Willem Van Onsem Jan 23 '17 at 02:50
  • @Vincenzzzochi: what I mean is, say I define a function `lambda x, y : x and y`, I have defined a function that is equivalent to `and`, can you tell me why `and` has no right to be a function itself? What is missing such that it cannot be a function? – Willem Van Onsem Jan 23 '17 at 03:00
  • `all` and `any` are boolean functions which always return `True` or `False`. – Uri May 30 '20 at 13:49
  • @Uri: but why? Since the `or` and `and` use the truthiness, that is a not very consistent. If you use `'' or 'a'`, you get `'a'`, then why can't `any(['', 'a'])` return `'a'` as well, since `any` is for examply in functional programming often defined as `any = foldr (||)`. – Willem Van Onsem May 30 '20 at 13:51
  • @WillemVanOnsem I think it makes more sense if they always return `True` or `False`. Then you never use their value except as a boolean value. – Uri May 30 '20 at 14:01
  • @uri: but if you wrap it in an `if`, then it checks the truthiness anyway. The only thing is that if you return it, then indeed it will not return `True` and `False`, but the question is, why are `True` and `False` "special". After all, a dynamic programming language is designed to have types at runtime. – Willem Van Onsem May 30 '20 at 14:02

0 Answers0