Reputation: 536
Dataclass is changing dict across objects in the following code. What can i do to not have that behaviour?
from dataclasses import dataclass, field
from typing import Dict
@dataclass
class Test:
param: Dict[str, int] = field(default_factory = lambda: ({"xyz": 0}))
test1_obj = Test()
test2_obj = Test()
test1_obj.param["xyz"] = 10
print(test1_obj.param["xyz"])
print(test2_obj.param["xyz"])
Both the lines printed return 10, while i wanted test2_obj to still return 0. How can i change in the construct of dataclass?
** this is fine behaviour on 3.7 version and higher **
Upvotes: 1
Views: 21718
Reputation: 536
As suggested by many users here, this works fine on python 3.7 and higher versions.
Upvotes: 2