I realize questions quite similar to this have been asked, though not exactly this way.
I'd like to have an optional argument for the constructor of my class that, if it is an instance of my class, will be copied. For example, something like (I know this code does not work!):
def __init__(self, foo=None):
self.x = None
self.y = None
self.z = None
if foo is not None and isinstance(foo, Foo):
self = copy.deepcopy(foo)
a = Foo()
a.x = 1
a.y = 2
a.z = 3
b = Foo(a)
b = copy.deepcopy(a)
I think this is the most pythonic way of doing it - a copy factory method.
import copy class Foo(object): def __init__(self): self.x = None self.y = None self.z = None def copy(self): return copy.deepcopy(self) a = Foo() a.x = 1 a.y = 2 a.z = 3 b = a.copy() print b.x print b.y print b.z
This is fairly common in python basic types (eg
dict.copy). It is not a copy constructor, but I don't think that is a very pythonic concept!