You should define a __copy__
method for your class. Adding eg.
def __copy__(self):
return MyClass(self.a, self.b)
to your code gives:
>>> import cp
>>> foo = cp.MyClass(5, 10)
>>> import copy
>>> bar = copy.copy(foo)
>>> bar == foo
True
As an alternative, if you want also to pickle your object you may want to implement The pickle protocol for extension type that is a __reduce__
method. Then it will be used for copying.