How to Resolve Python "TypeError: 'bool' object is not callable"
The TypeError: 'bool' object is not callable
is a common error in Python indicating that you've attempted to use parentheses ()
to call something that holds a boolean value (True
or False
) as if it were a function. Functions and methods are callable, but simple boolean values are not.
This guide explains the common scenarios that lead to this error and provides clear solutions to fix your code.
Understanding the Error: Calling Non-Callables
In Python, parentheses ()
after a name signify a function or method call. You use them to execute the code within that function/method. A TypeError: '...' object is not callable
occurs when the object preceding the parentheses is not something that can be executed, like a simple integer, string, or, in this case, a boolean (True
or False
).
my_bool_value = True
another_value = False
print(type(my_bool_value)) # Output: <class 'bool'>
try:
# ⛔️ TypeError: 'bool' object is not callable
# Trying to "call" the value True
my_bool_value()
except TypeError as e:
print(e)
try:
# ⛔️ TypeError: 'bool' object is not callable
# Trying to "call" the value False
another_value()
except TypeError as e:
print(e)
You can not execute True
or False
as if they were functions.
Cause 1: Adding Parentheses to a Boolean Variable
The most straightforward cause is accidentally adding parentheses when you simply meant to check the value of a boolean variable, often within an if
statement.
is_logged_in = True
# Error Scenario
try:
# ⛔️ TypeError: 'bool' object is not callable
# Incorrectly added () after the boolean variable
if is_logged_in():
print("User is logged in.")
except TypeError as e:
print(f"Error: {e}")
# Solution: Remove the parentheses
if is_logged_in:
# This block runs
print("User is logged in (Correct check).")
else:
print("User is not logged in (Correct check).")
When checking a boolean's truthiness, use the variable directly without calling it.
Cause 2: Calling a Function That Returns a Boolean Twice
If you have a function that returns a boolean, calling it once gives you True
or False
. Adding a second set of parentheses attempts to call that returned boolean value.
def check_status():
print("Checking status...")
return True # This function returns a boolean
# Error Scenario
try:
# ⛔️ TypeError: 'bool' object is not callable
# check_status() returns True. The second () tries to call True().
result = check_status()()
print(f"Result: {result}")
except TypeError as e:
print(f"Error: {e}")
# Solution: Call the function only to get the boolean result
result = check_status() # Removed the extra pair of parentheses
print(f"Result (Correct call): {result}") # Output: True
if result:
print("Status check passed.")
Cause 3: Variable Name Clashing with Function Name
If you define a function and later create a variable with the exact same name, the variable name "shadows" or hides the function name. Subsequent attempts to call the function will actually try to call the variable's value.
def send_message():
print("Sending message...")
return True
# Function call works fine here:
send_message()
# --- Later in the code ---
# ⚠️ Variable assigned the same name as the function
send_message = False # Now 'send_message' refers to False, not the function
# Error Scenario
try:
# ⛔️ TypeError: 'bool' object is not callable
# This now tries to call the boolean value False assigned above.
status = send_message()
print(f"Status: {status}")
except TypeError as e:
print(f"Error: {e}")
# Solution: Rename the variable (or the function)
def send_message_func(): # Renamed function (or rename variable below)
print("Sending message (fixed)...")
return True
message_sent_status = False # Use a different variable name
# ✅ Now calling the function works correctly
status_fixed = send_message_func()
print(f"Status (fixed): {status_fixed}") # Output: True
Use distinct names for variables and functions to avoid these clashes.
Cause 4: Shadowing the Built-in bool
Type
Accidentally naming a variable bool
overrides Python's built-in bool()
type constructor. If you later try to use the actual bool()
function (e.g., for type conversion), you'll be trying to call the value you assigned to your variable.
# Error Scenario
# ⚠️ Assigning a boolean value to the name 'bool', hiding the built-in
bool = True
try:
# ⛔️ TypeError: 'bool' object is not callable
# This tries to call the variable 'bool' (which is True), not the type constructor
is_non_empty = bool("hello")
print(is_non_empty)
except TypeError as e:
print(e)
# Solution: Use a different variable name
# ✅ Avoid using built-in names for variables
my_custom_bool = True
# ✅ Now bool() refers to the built-in type constructor again
is_non_empty_fixed = bool("hello")
print(f"Is 'hello' truthy? {is_non_empty_fixed}") # Output: True
Never use built-in type names (like bool
, list
, dict
, str
, int
, etc.) as variable names.
Restart your script or kernel if you've accidentally done this in an interactive session.
Cause 5: Class Attribute Name Clashing with Method Name
Within a class, if an instance attribute (set in __init__
or directly on the instance) has the same name as a method, accessing that name on an instance will typically retrieve the attribute value first, hiding the method. Trying to call it will result in the error if the attribute holds a boolean.
class User:
def __init__(self, is_admin):
# Instance attribute named 'is_admin'
self.is_admin = is_admin # Holds True or False
# Method with the *same name* as the attribute
def is_admin(self):
print("Method is_admin called!") # This won't be reached easily
return "Admin Status Method"
# Error Scenario
user_instance = User(True)
print(f"Attribute value: {user_instance.is_admin}") # Accesses the attribute -> True
try:
# ⛔️ TypeError: 'bool' object is not callable
# user_instance.is_admin resolves to the attribute (True).
# Calling True() causes the error.
status = user_instance.is_admin()
print(status)
except TypeError as e:
print(f"Error: {e}")
# Solution: Rename the method or the attribute
class UserFixed:
def __init__(self, is_admin_flag):
# Use a different attribute name (e.g., _is_admin or is_admin_flag)
self.is_admin_flag = is_admin_flag
# Or rename the method (more common convention)
def check_admin_status(self):
print("Method check_admin_status called!")
return self.is_admin_flag
user_fixed = UserFixed(True)
# ✅ Call the distinct method name
status_fixed = user_fixed.check_admin_status()
print(f"Status (fixed): {status_fixed}") # Output: True (from the flag)
Use different names for instance attributes and methods within your classes. Prefixing "internal" attributes with an underscore (self._is_admin
) is a common convention.
Debugging Tip: Check Variable Types
If you're unsure why the error is happening, print the type of the object you are trying to call right before the line that raises the error:
variable_causing_error = ... # The variable involved in the error
print(f"Type before call: {type(variable_causing_error)}")
# Attempt the call that causes the error:
try:
variable_causing_error()
except TypeError as e:
print(f"Error confirms type issue: {e}")
If the output shows <class 'bool'>
, you know you need to apply one of the solutions above.
Conclusion
The TypeError: 'bool' object is not callable
arises when you attempt to execute True
or False
using call parentheses ()
. The primary solutions involve:
- Removing unnecessary parentheses when checking a boolean variable's value (
if my_bool:
instead ofif my_bool():
). - Avoiding double function calls (
my_func()
instead ofmy_func()()
). - Using distinct names for variables and functions to prevent shadowing.
- Never overriding built-in type names like
bool
. - Using distinct names for class attributes and methods.
By identifying which of these scenarios applies to your code, you can quickly resolve this common Python error.