Borislav Hadzhiev
Wed Apr 20 2022·2 min read
Photo by Nazar Magellan
The Python "AttributeError: 'dict' object has no attribute" occurs when we use
dot notation instead of bracket notation to access a key in a dictionary. To
solve the error, use bracket notation when accessing the key, e.g.
my_dict['age']
.
Here is an example of how the error occurs.
my_dict = {'name': 'Alice', 'age': 30} # ⛔️ AttributeError: 'dict' object has no attribute 'name' print(my_dict.name)
If you are trying to access a key in a dictionary, use bracket notation.
my_dict = {'name': 'Alice', 'age': 30} # ✅ using bracket notation print(my_dict['name']) # 👉️ "Alice" print(my_dict['age']) # 👉️ 30 # ✅ using get() method to avoid errors if key not in dict print(my_dict.get('name')) # 👉️ "Alice" print(my_dict.get('age')) # 👉️ 30
The get()
method doesn't raise a KeyError if the key is not present in the
dictionary.
dict
class.If you are trying to add key-value pairs to a dictionary, use bracket notation.
my_dict = {'name': 'Alice', 'age': 30} my_dict['country'] = 'Austria' my_dict['job'] = 'programmer' # 👇️ {'name': 'Alice', 'age': 30, 'country': 'Austria', 'job': 'programmer'} print(my_dict)
The bracket notation syntax can be used to both access the value of a key and set or update the value for a specific key.
If you need to iterate over a dictionary, use the items()
method.
my_dict = {'name': 'Alice', 'age': 30} for key, value in my_dict.items(): print(key, value)
Make sure you aren't reassigning the value of your variable somewhere in your code.
my_list = ['a', 'b'] # 👇️ reassign list to a dictionary by mistake my_list = {'name': 'Alice'} # ⛔️ AttributeError: 'dict' object has no attribute 'append' my_list.append('c')
We set the value of the my_list
variable to a list initially, but we set it to
a dictionary later on, which caused the error.
If you need to check whether an object contains an attribute, use the hasattr
function.
my_dict = {'name': 'Alice', 'age': 30} if hasattr(my_dict, 'values'): print(my_dict.values()) # 👉️ dict_values(['Alice', 30]) else: print('Attribute not present in object')
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.
A good way to start debugging is to print(dir(your_object))
and see what
attributes a dictionary has.
Here is an example of what printing the attributes of a dict
looks like.
my_dict = {'name': 'Alice', 'age': 30} # [...'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', # 'pop', 'popitem', 'setdefault', 'update', 'values' ...] print(dir(my_dict))
If you pass a class to the dir() function, it returns a list of names of the classes' attributes, and recursively of the attributes of its bases.
If you try to access any attribute that is not in this list, you would get the "AttributeError: 'dict' object has no attribute error".