I just started learning Python and have a question about dataclass initialization and updating its field using a function.
When I run the code below, I was expecting to second.flags.checked
to be None
, but I see it set to True
instead. What is the correct way to initialize a dataclass so that new Flags
is initialized I create a new TestClass
in this case?
class Flags:
checked: bool = None
@dataclass
class TestClass:
test: str
flags: Flags = Flags()
def update_flags(self):
self.flags.checked = True
first = TestClass(test="test1")
first.update_flags()
second = TestClass(test="test2")
print(f"{second.flags.checked}")