AttributeError: 'bool' object has no attribute 'X' in Python

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

# AttributeError: 'bool' object has no attribute 'X' in Python

The Python "AttributeError: 'bool' object has no attribute" occurs when we try to access an attribute on a boolean value (True or False).

To solve the error, track down where you are setting the value to a boolean or use the hasattr() method to check for the attribute's existence.

attributeerror bool object has no attribute

Here is an example of how the error occurs.

main.py
example = True # โ›”๏ธ AttributeError: 'bool' object has no attribute 'my_attribute' print(example.my_attribute)

We are trying to access an attribute on a boolean (True or False) instead of on an object.

If you print() the value you are accessing the attribute on, it will be a boolean.

# Track down where the variable got assigned a boolean value

To solve the error, you need to track down where exactly you are setting the value to a boolean in your code and correct the assignment.

For example, you might be returning a boolean value from a function somewhere in your code and then assigning that to the variable instead of an object.

main.py
def get_obj(): return False # ๐Ÿ‘‡๏ธ False example = get_obj() # โ›”๏ธ AttributeError: 'bool' object has no attribute 'items' print(example.items())

returning boolean and assigning it to variable

The get_obj function in the example returns a boolean, so the example variable gets assigned a False value which causes the error.

When you compare values, you get a boolean result.

main.py
result = 10 == 15 print(result) # ๐Ÿ‘‰๏ธ False result = len('bobbyhadz.com') > 5 print(result) # ๐Ÿ‘‰๏ธ True

Trying to access an attribute on a True or a False value causes the error.

# Reassigning the variable in your code by mistake

Make sure you haven't reassigned the variable to a boolean (True or False) by mistake.

main.py
a_str = 'bobbyhadz.com' # ๐Ÿ‘‡๏ธ Reassigning the variable to a boolean a_str = True # โ›”๏ธ AttributeError: 'bool' object has no attribute result = a_str.upper()

reassigning the variable by mistake

We initially declared the variable and set it to a string, but it later got assigned a boolean value which caused the error.

# Check if the object contains the attribute before accessing it

If you need to check whether an object contains an attribute, use the hasattr function.

main.py
example = True if hasattr(example, 'my_attribute'): print(example.my_attribute) else: print('Attribute is not present on object') # ๐Ÿ‘‰๏ธ this runs

check if object contains attribute before accessing it

The hasattr function takes the following 2 parameters:

NameDescription
objectThe object we want to test for the existence of the attribute
nameThe name of the attribute to check for in the object

The hasattr() function returns True if the string is the name of one of the object's attributes, otherwise False is returned.

Using the hasattr function would handle the error if the attribute doesn't exist on the object, however, you still have to figure out where the variable gets assigned a boolean value in your code.

main.py
class Employee(): def __init__(self, name): self.name = name employee = Employee('Bobby Hadz') # ๐Ÿ‘‡๏ธ Assignment to boolean here (cause of the error) employee = True # โ›”๏ธ AttributeError: 'bool' object has no attribute 'name' print(employee.name)

The example shows how the employee variable stored an object at first but got assigned a boolean value somewhere in our code which caused the error.

If we remove the line that assigns a boolean value to employee in the example, the error wouldn't occur.

main.py
class Employee(): def __init__(self, name): self.name = name employee = Employee('Bobby Hadz') print(employee.name) # ๐Ÿ‘‰๏ธ Bobby Hadz

If you print() the value you are accessing the attribute on and get a True or False value back, you are either assigning the incorrect value to a variable or you have a re-assignment somewhere in your code that overrides the object with a boolean value.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev