I generate two different instances of a python dataclass
which includes a nested dataclass
. When I update a value in the nested dataclass
in one instance (but not in the other), the same data is placed in the nested dataclass
in both instances. This is not what I expected.
from dataclasses import dataclass
@dataclass
class sub1:
q: int = 10
r: str = "qrst"
@dataclass
class A:
a: int = 1
s1: sub1 = sub1()
if __name__ == '__main__':
a = A()
aa = A()
aa.a = 9
aa.s1.r = "92"
print("a:", repr(a))
print("aa:", repr(aa))
''' Produces --
a: A(a=1, s1=sub1(q=10, r='92'))
aa: A(a=9, s1=sub1(q=10, r='92'))
'''
I expected the nested dataclass
to be updated in only the specified instance (aa
) and for the nested dataclass
in the other instance (a
) to remain unchanged.
What am I doing wrong, or is dataclass
the wrong tool?