[docs] misbehaving functionality of all() function

Julien Palard julien at palard.fr
Sun May 26 05:03:50 EDT 2019

Hi Sai,

> if one of the element(1+12.1j) is not iterable

This does not make sense to me, 1+12.1j does not have "elements", it's a single value.

> TypeError: 'complex' object is not iterable

As I just said, 1+12.1j is not iterable (it does not have elements), Python agree with me.

> My concern is why all([1,2,1+12.1j]) is returning True if all elements are not iterable.

all expects a single iterable, which is the list of three values, (starting with [ and ending with ]).
all returns true if all values (in this case the value 1, the value 2, and the value 1+12.1j) are truthy, which is the case.

You can imagine all being implemented like this (it is not, as the builtin all is implemented in C, but they behave the same):

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

So when you call all(1+12.1j) you're basically asking Python to do:

for element in 1+12.1j:

which does not work, 1+12.1j is not iterable, it's a single value, not a collection of values.

when you call all([1,2,1+12.1j]) you're basically asking Python to do:

for element in [1,2,1+12.1j]:
    if not element:  # Tested with 1, then with 2, then with 1+12.1j, all are True, so no return False.
        return False
return True  # This line will be reached, the result of all([1,2,1+12.1j]) will be True.

Hope it helps.
Julien Palard

More information about the docs mailing list