Last updated: Apr 8, 2024
Reading timeยท3 min
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.
Here is an example of how the error occurs.
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.
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.
def get_obj(): return False # ๐๏ธ False example = get_obj() # โ๏ธ AttributeError: 'bool' object has no attribute 'items' print(example.items())
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.
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.
Make sure you haven't reassigned the variable to a boolean (True
or False
)
by mistake.
a_str = 'bobbyhadz.com' # ๐๏ธ Reassigning the variable to a boolean a_str = True # โ๏ธ AttributeError: 'bool' object has no attribute result = a_str.upper()
We initially declared the variable and set it to a string, but it later got assigned a boolean value which caused the error.
If you need to check whether an object contains an attribute, use the hasattr
function.
example = True if hasattr(example, 'my_attribute'): print(example.my_attribute) else: print('Attribute is not present on object') # ๐๏ธ this runs
The hasattr function takes the following 2 parameters:
Name | Description |
---|---|
object | The object we want to test for the existence of the attribute |
name | The 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.
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.
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.