In "How to override the copy/deepcopy operations for a Python object?" post, it is written in one of the answers that:
def __deepcopy__(self, memo):
cls = self.__class__
result = cls.__new__(cls)
memo[id(self)] = result
for k, v in self.__dict__.items():
setattr(result, k, deepcopy(v, memo))
return result
Is a possible way of overriding the deepcopy method. I do not understand what each of the lines does. What is the purpose of this implementation? Can't I just create my own copy method with something specific for my class like the following?
def my_copy(self, original_obj):
obj = MyClass()
obj.a = (copy of a from the other object)
obj.b = (copy of b from the other object)
...
return obj