D
D
Delicious Khachapuri2021-11-30 18:44:58
Python
Delicious Khachapuri, 2021-11-30 18:44:58

When might it be necessary to define a deepcopy() method when declaring a custom class in Python?

Hello! I'm learning Python, a question arose in the process of parsing the copy module.

Question: When might it be necessary to define a __deepcopy__() method when declaring a custom class? Interested in both specific examples from practice, and just an abstract explanation.

Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
LXSTVAYNE, 2021-11-30
@lxstvayne

This method is defined to take action when deep copying an object, by the deepcopy function from the copy module. Let's say you wrote your container, it stores a list of something. Python needs to know how to create a deep copy of your instance, which is why the __deepcopy__ magic method is overridden.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question