Python all() function is one of the built-in functions. It takes iterable as an argument and returns True
if all elements of the iterable are true or it’s empty.
Python all() function
Python all() function is a utility method and shortcut to below function.
1 2 3 4 5 6 7 |
def all(iterable): for element in iterable: if not element: return False return True |
Let’s look at some of the examples of python all() function.
Python all() example with boolean
1 2 3 4 5 6 7 8 |
# iterable has all True list_bools = [True, True, True] print(all(list_bools)) # iterable all elements are not True list_bools = [True, True, False] print(all(list_bools)) |
Output:
1 2 3 4 |
True False |
Python all() with empty iterable
1 2 3 4 5 |
# iterable is empty list_bools = [] print(all(list_bools)) |
Output:
1 2 3 |
True |
Python all() with list of strings
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# iterable elements are True string list_strs = ['True', 'True'] print(all(list_strs)) # iterable all elements are true string with different case list_strs = ['True', 'true'] print(all(list_strs)) # iterable all elements are not true string list_strs = ['abc', 'true'] print(all(list_strs)) # iterable all elements are empty string list_strs = ['', 'true'] print(all(list_strs)) |
Output:
1 2 3 4 5 6 |
True True True False |
When we want an object boolean value, python looks for __bool__
function in the object.
If __bool__
function is not defined, then len()
function is called if it’s defined. The object boolean value is considered as True if len()
output is non-zero.
If a class defines neither __len__()
nor __bool__()
functions, all its instances are considered True.
Python 3 uses __bool__
function to check object boolean value, if you are using python 2.x then you have to implement __nonzero__
function.
Python all() with custom objects
Let’s test above explanation with a custom class. We will create a custom Person class and use its objects in the list and call all() function on it.
1 2 3 4 5 6 7 8 9 10 |
class Person: name = "" def __init__(self, n): self.name = n list_objs = [Person("Pankaj"), Person("Lisa")] print(all(list_objs)) list_objs = [Person("A"), Person("David")] print(all(list_objs)) |
Output:
1 2 3 4 |
True True |
Since our object doesn’t have __len__() and __bool__() function defined, it’s boolean value is True.
Let’s go ahead and define __len__() function for the Person class as below.
1 2 3 4 5 |
def __len__(self): print('len function called') return len(self.name) |
Now the output of earlier code snippets will be:
1 2 3 4 5 6 7 8 |
len function called len function called True len function called len function called True |
Notice that len()
function is getting called for each object when all() is used with the list of Person objects.
Now let’s define __bool__
function for the Person class and see what happens with the above code.
1 2 3 4 5 6 7 8 |
def __bool__(self): print('bool function called') if len(self.name) > 3: return True else: return False |
Output:
1 2 3 4 5 6 7 |
bool function called bool function called True bool function called False |
It’s clear from the output that if __bool__ function is defined, then it’s used for getting the python object boolean value. Notice that second list all() function output is False because ‘A’ length is less than 3.
That’s all for python all() function examples.
Reference: Official Documentation