Reputation: 197
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?
Upvotes: 6
Views: 5472
Reputation: 60994
What you are currently doing is providing a default value for the field. As that value is a mutable object, changes to that object will be visible to all instances of your dataclass.
What you should do instead is provide a default factory that produces sub1
instances for each new A
instance:
from dataclasses import field
@dataclass
class A:
a: int = 1
s1: sub1 = field(default_factory=sub1)
a = A()
aa = A()
aa.a = 9
aa.s1.r = "92"
print("a:", repr(a)) # a: A(a=1, s1=sub1(q=10, r='qrst'))
print("aa:", repr(aa)) # aa: A(a=9, s1=sub1(q=10, r='92'))
Upvotes: 12