I have two classes: a parent class and a container class. The parent class instance has matching container class instance as a weak reference.
There is a problem while deep copying the parent instance, the weakref is still linking to the original instance. Here is a minimal example:
import weakref
from copy import deepcopy
class Container:
def __init__(self, parent):
self.parent = weakref.ref(parent)
class Parent:
def __init__(self):
self.container = Container(self)
if __name__ == '__main__':
parent1 = Parent()
assert(parent1 is parent1.container.parent())
parent2 = deepcopy(parent1)
assert(parent2 is parent2.container.parent())
The second assertion fails.
I suspect that the __deepcopy__ magic method can be implemented, but not sure how exactly.