I have instances of a dataclass that reference each other.
from dataclasses import dataclass
@dataclass()
class Foo:
id: int
neighbor: 'Foo'
foo = Foo(1, None)
bar = Foo(2, foo)
foo.neighbor = bar
I really want a frozen class, since these objects must not be manipulated in a multi threaded run. But if I declare frozen=True
, the last line will obviously raise an error. I cannot think of a way how to deal with this. I read this thread but the solution does not work for me, since foo.neighbor
should point at another frozen instance.
Is there any way to achieve this? I am not bound to dataclasses. But I encounter the same problem using namedtuples.