Answer the question
In order to leave comments, you need to log in
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
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 questionAsk a Question
731 491 924 answers to any question