ThunderPhoenix
ThunderPhoenix

Reputation: 1883

When should we implement `__deepcopy__`?

I would like to know in what situations is interesting/necessary to redefine __deepcopy__? Indeed, this method is already implemented in a such a way to perform a deepcopy of an object.

So what is the point of redefining it differently?

Upvotes: 2

Views: 86

Answers (1)

Joshua Fox
Joshua Fox

Reputation: 19655

If there are cyclic references in the object-graph, things get tricky. The built-in __deepcopy__ does in fact handle these, but you may want to customize the approach.

Also, there are objects (the original that you are calling deepcopy on or one in its reference graph) that simply cannot be deep-copied, like file handles, and again, you may want your own approach.

Upvotes: 3

Related Questions