Comment on Loading Pydantic models from JSON without running out of memoryparentComments−itamarstOP1yYou can't add extra attributes that weren't part of the original dataclass definition: >>> from dataclasses import dataclass >>> @dataclass ... class C: pass ... >>> C().x = 1 >>> @dataclass(slots=True) ... class D: pass ... >>> D().x = 1 Traceback (most recent call last): File "<python-input-4>", line 1, in <module> D().x = 1 ^^^^^ AttributeError: 'D' object has no attribute 'x' and no __dict__ for setting new attributes Most of the time this is not a thing you actually need to do.−masklinn1yAlso some of the introspection stops working e.g. vars().If you're using dataclasses it's less of an issue because dataclasses.asdict.−monomial1yI rarely need to dynamically add attributes myself on dataclasses like this but unfortunately this also means things like `@cached_property` won't work because it can't internally cache the method result anywhere.−franga20001yIIRC you can just include a __dict__ slot and @cached_property should start working again. I
Comments
You can't add extra attributes that weren't part of the original dataclass definition:
Most of the time this is not a thing you actually need to do.Also some of the introspection stops working e.g. vars().
If you're using dataclasses it's less of an issue because dataclasses.asdict.
I rarely need to dynamically add attributes myself on dataclasses like this but unfortunately this also means things like `@cached_property` won't work because it can't internally cache the method result anywhere.
IIRC you can just include a __dict__ slot and @cached_property should start working again. I