How to Solve Python TypeError: ‘bool’ object is not callable

by | Programming, Python, Tips

The TypeError ‘bool’ object is not callable occurs when you try to call a Boolean by putting parenthesis () after it like a function. Only functions respond to function calls.

This tutorial will go through the error in detail and how to solve it with the help of code examples.


TypeError: ‘bool’ object is not callable

Calling a function means the Python interpreter executes the code inside the function. In Python, we can only call functions. We can call functions by specifying the name of the function we want to use followed by a set of parentheses, for example, function_name(). Let’s look at an example of a working function that returns a string.

# Declare function

def simple_function():

    print("Learning Python is fun!")

# Call function

simple_function()
Learning Python is fun!

We declare a function called simple_function in the code, which prints a string. We can then call the function, and the Python interpreter executes the code inside simple_function().

Bool type objects do not respond to a function call because they are not functions. If you try to call a Bool type object if it were a function, the Python interpreter will raise the TypeError: ‘bool’ object is not callable.

We can verify if an object is callable by using the built-in callable() method and passing the object to it. If the method returns True, then the object is callable, otherwise, if it returns False the object is not callable. Let’s look at testing the method with a Boolean:

a_bool = True

print(callable(a_bool))
False

The callable function returns false for a Boolean, verifying that bool objects are not callable.

Example

Let’s look at an example where we define a function that checks if a number is a prime number. We will use this function to check if a list of numbers contains prime numbers. First, let’s look at the function, which we will call prime_number.

def prime_number(num):

    # Is prime number flag

    is_prime = False

    # Prime numbers are greater than 1

    if num > 1:

        # Check for factors

        for i in range(2, num):

            #If factor is found, set is_prime to True

            if (num % i) == 0:

                is_prime = True

                # break out of the loop

                break

    return is_prime

The function takes in one argument, which is the number we want to check, and returns True if the number is prime and False if it is not. Next, we will iterate over a list of numbers and pass each number to a prime_number function call.

lst = [4, 7, 12, 17, 23, 44]

for i in lst:

    prime_number = prime_number(i)

    print(f'Is {i} Prime? {prime_number}')

Let’s run the code to see what happens:

Is 4 Prime? False
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-cb5177ccdebb> in <module>
      2 
      3 for i in lst:
----> 4     prime_number = prime_number(i)
      5     print(f'Is {i} Prime? {prime_number}')

TypeError: 'bool' object is not callable

We get a TypeError because the variable prime_number has the same name as the function we want to call. The variable gets assigned a Boolean value for the first loop.

Because the variable has the same name as the function, the Boolean value overrides the function.

Then, in the second loop, when we try to call the prime_number() function we are calling the Boolean value from the previous loop instead.

We can verify the override by checking the type of prime_number using type().

lst = [4, 7, 12, 17, 23, 44]

for i in lst:

    print(type(prime_number))

    prime_number = prime_number(i)

    print(type(prime_number))

    print(f'Is {i} Prime? {prime_number}')
<class 'function'>
<class 'bool'>
Is 4 Prime? True
<class 'bool'>

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-7-5ba50fe7142f> in <module>
      3 for i in lst:
      4     print(type(prime_number))
----> 5     prime_number = prime_number(i)
      6     print(type(prime_number))
      7     print(f'Is {i} Prime? {prime_number}')

TypeError: 'bool' object is not callable

We see that at the start of the loop, prime_number is a function, and then after the first call prime_number is Boolean. Then at the start of the second loop, when we want to make a function call, prime_number is still a Boolean.

Solution

To solve this error, we need to use a distinct variable name. We will choose is_prime instead of prime_number. If you are using IPython, ensure you start from a new session or redefine the prime_number function. Let’s look at the revised code:

lst = [4, 7, 12, 17, 23, 44]

for i in lst:

    is_prime = prime_number(i)

    print(f'Is {i} Prime? {is_prime}')

Let’s run the code to see the result:

Is 4 Prime? True
Is 7 Prime? False
Is 12 Prime? True
Is 17 Prime? False
Is 23 Prime? False
Is 44 Prime? True

We can also verify that prime_number stays as a function during the entire program lifecycle:

lst = [4, 7, 12, 17, 23, 44]

for i in lst:

    print(type(prime_number))

    is_prime = prime_number(i)

    print(type(prime_number))

    print(f'Is {i} Prime? {is_prime}')

Let’s run the code to see the result:

<class 'function'>
<class 'function'>
Is 4 Prime? True
<class 'function'>
<class 'function'>
Is 7 Prime? False
<class 'function'>
<class 'function'>
Is 12 Prime? True
<class 'function'>
<class 'function'>
Is 17 Prime? False
<class 'function'>
<class 'function'>
Is 23 Prime? False
<class 'function'>
<class 'function'>
Is 44 Prime? True

Summary

Congratulations on reading to the end of this tutorial. The TypeError ‘bool’ object is not callable occurs when you try to call a Boolean as if it were a function. TypeErrors occur when you attempt to perform an illegal operation for a specific data type.

To solve this error, ensure that the variable names and function names are distinct and that there are no parentheses after Boolean values. You can check if an object is a Boolean by using the built-in type() method.

For further reading on not callable TypeErrors, go to the articles:

To learn more about Python, specifically for data science and machine learning, go to the online courses page on Python.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee ✨