AttributeError: 'dict' object has no attribute 'deepcopy'

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

AttributeError: 'dict' object has no attribute 'deepcopy' #

The Python "AttributeError: 'dict' object has no attribute 'deepcopy'" occurs when we try to call the deepcopy() method on a dictionary. To solve the error, import the copy module and use the deepcopy method, e.g. result = copy.deepcopy(my_dict).

attributeerror dict object has no attribute deepcopy

Here is an example of how the error occurs.

main.py
employee = { "id": 1, "name": "Alice", "address": { "country": "Austria", "city": "Linz" } } # ⛔️ AttributeError: 'dict' object has no attribute 'deepcopy' employee_copy = employee.deepcopy()

Dictionaries don't have a deepcopy method in Python, however we can use the deepcopy method from the copy module.

main.py
import copy employee = { "id": 1, "name": "Alice", "address": { "country": "Austria", "city": "Linz" } } result = copy.deepcopy(employee) # 👇️ {'id': 1, 'name': 'Alice', 'address': {'country': 'Austria', 'city': 'Linz'}} print(result)

The copy.deepcopy method returns a copy of the passed in object.

The difference between a shallow and deep copying is that with shallow copying the references for nested objects are preserved.

If we change a nested object of a deeply copied dictionary, the original dictionary will remain unchanged.

main.py
import copy employee = { "id": 1, "name": "Alice", "address": { "country": "Austria", "city": "Linz" } } result = copy.deepcopy(employee) result['address']['country'] = 'Belgium' print(result['address']['country']) # 👉️ "Belgium" print(employee['address']['country']) # 👉️ "Austria"

The dict.copy() method creates a shallow copy of the dictionary.

main.py
employee = { "id": 1, "name": "Alice", "address": { "country": "Austria", "city": "Linz" } } shallow = employee.copy() shallow['address']['country'] = 'Belgium' print(shallow['address']['country']) # 👉️ "Belgium" print(employee['address']['country']) # 👉️ "Belgium"

Notice that changing a nested object in the shallow copy dictionary also changes the original.

This is because the nested dictionary points to the same location in memory in both the shallow copy and the original.

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.

main.py
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 class's 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".

Since dict objects don't have a deepcopy() method, the error is caused.

Conclusion #

The Python "AttributeError: 'dict' object has no attribute 'deepcopy'" occurs when we try to call the deepcopy() method on a dictionary. To solve the error, import the copy module and use the deepcopy method, e.g. result = copy.deepcopy(my_dict).

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.