Solve - TypeError: 'bool' object is not callable in Python

avatar

Borislav Hadzhiev

Wed Apr 20 20222 min read

Solve - TypeError: 'bool' object is not callable in Python #

The Python "TypeError: 'bool' object is not callable" occurs when we try to call a boolean value (True or False) as a function. To solve the error, correct the assignment and resolve any clashes between function and variable names.

typeerror bool object is not callable

Here is an example of how the error occurs.

main.py
example = True # ⛔️ TypeError: 'bool' object is not callable example()
You have to track down where the variable got assigned a boolean value (True or False) and correct the assignment to a function or class, or remove the parenthesis.

Make sure you aren't overriding the built-in bool() function.

main.py
# 👇️ overrides built-in bool() function bool = True # ⛔️ TypeError: 'bool' object is not callable print(bool('hello'))

We override the built-in bool function setting it to a boolean primitive and try to call it as a function.

To solve the error, make sure to rename your variable and restart your script.

main.py
# ✅ Not overriding built-in bool() anymore my_bool = True print(bool('hello'))

Make sure you aren't trying to call a function that returns a boolean twice.

main.py
def is_subscribed(): return True # ⛔️ TypeError: 'bool' object is not callable is_subscribed()() # 👈️ remove extra set of parenthesis

Another common cause of the error is having a function and a variable that share the same name.

main.py
def example(): return 'hello world' example = True # ⛔️ TypeError: 'bool' object is not callable example()

The example variable shadows the function with the same name, so when we try to call the function, we actually end up calling the variable.

Renaming the variable or the function solves the error.

The error is also caused when we have a class method and a class property with the same name.

main.py
class Employee(): def __init__(self, is_subscribed): self.is_subscribed = is_subscribed # 👇️ same name as class variable def is_subscribed(self): return self.is_subscribed emp = Employee(False) # ⛔️ TypeError: 'bool' object is not callable print(emp.is_subscribed())

The Employee class has a method and an attribute with the same name.

The attribute hides the method, so when we try to call the method on an instance of the class, we get the object is not callable error.

To solve the error, you have to rename the class method.

main.py
class Employee(): def __init__(self, is_subscribed): self.is_subscribed = is_subscribed def get_is_subscribed(self): return self.is_subscribed emp = Employee(False) print(emp.get_is_subscribed()) # 👉️ False

Once you rename the method, you will be able to call it without any issues.

Use the search field on my Home Page to filter through my more than 1,000 articles.