Does this look like your situation?
import copy class Foo(object): shared = [[]] def __init__(self): self.perinstance = [[]]
If so, then you do not need to define __copy__
or __deepcopy__
, since the default behavior of copy.deepcopy
separates class attributes and instance attributes of deepcopies:
x = Foo() z = copy.deepcopy(x) assert id(z.shared) == id(x.shared) assert id(z.shared[0]) == id(x.shared[0]) assert id(z.perinstance) != id(x.perinstance) assert id(z.perinstance[0]) != id(x.perinstance[0])
source share