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.