Python bool()

Filed Under: Python

Python bool() function returns Boolean value for an object. The bool class has only two instances – True and False. This class can’t be extended.

Python bool()

Python bool() function uses standard truth testing rules to convert the specified argument object to Boolean value.

Some of the rules used to return Boolean value are:

  • Any object Boolean value is considered true if it’s not implementing __bool__() function and __len__() functions.
  • If the object doesn’t define __bool__() function but defines __len__() function, then __len__() function is used to get the boolean value of object. If __len__() returns 0, then bool() function will return False otherwise True.
  • Boolean value will be False for None and False constants.
  • Boolean value will be False for zero value numbers such as 0, 0.0, 0j, Decimal(0), and Fraction(0, 1).
  • Boolean value will be False for empty sequences (tuple, dict) and collections, such as ”, (), [], {} etc.

Python bool() example

Let’s look at some simple examples of bool() with bool instances and None.


x = True
b = bool(x)

print(type(x))  # <class 'bool'>
print(type(b))  # <class 'bool'>
print(b)  # True

x = False
b = bool(x)
print(b)  # False

x = None
b = bool(x)

print(type(x))  # <class 'NoneType'>
print(type(b))  # <class 'bool'>
print(b)  # False

The output is self-explained and provided in the comments.

Python bool() with strings


# string examples
x = 'True'
b = bool(x)

print(type(x))  # <class 'str'>
print(type(b))  # <class 'bool'>
print(b)  # True

x = 'False'
b = bool(x)
print(b)  # True because len() is used

x = ''
print(bool(x))  # False, len() returns 0

Python bool() with numbers


print(bool(10))  # True
print(bool(10.55))  # True
print(bool(0xF))  # True
print(bool(10 - 4j))  # True

print(bool(0))  # False
print(bool(0.0))  # False
print(bool(0j))  # False
print(bool(Decimal(0)))  # False
print(bool(Fraction(0, 2)))  # False

Python bool() function with collections and sequences


tuple1 = ()
dict1 = {}
list1 = []
print(bool(tuple1))  # False
print(bool(dict1))  # False
print(bool(list1))  # False

Python bool() function with custom object

Let’s see what happens with custom object. I will not define __bool__() and __len__() functions for the object.


class Data:
    id = 0

    def __init__(self, i):
        self.id = i

d = Data(0)
print(bool(d))
d = Data(10)
print(bool(d))

Output:


True
True

Since none of __bool__() and __len__() functions are defined, object boolean value is returned as True.

Let’s add __len__() function to the Data class.


    # returns 0 for id <= 0, else id
    def __len__(self):
        print('len function called')
        if self.id > 0:
            return self.id
        else:
            return 0

Output:


len function called
False
len function called
True

It’s clear that __len__() function is called by bool(). When 0 is returned, bool() function is returning False. When positive integer is returned, then bool() function is returning True.

Now let’s add __bool__() function to Data class:


    # returns True for id > 0 else False
    def __bool__(self):
        print('bool function called')
        return self.id > 0

Now the above snippet output will be:


bool function called
False
bool function called
True

It’s clear from the output that if both __bool__() and __len__() functions are defined for the object, then __bool__() function is used to get the Boolean value of object.

You can checkout complete python script and more Python examples from our GitHub Repository.

Reference: Official Documentation

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages