0

Is it actually possible to have a field x which can get set in the constructor but is frozen with dataclasses in python?

@dataclass
class A():
  x : int


a = A(x=4)
a.x = 4      # Would raise an exception because its read-only.

I could not find out how that should work with @property or any other feature?

Gabriel
  • 8,990
  • 6
  • 57
  • 101
  • Does this answer your question? [How to make attribute in dataclass read-only?](https://stackoverflow.com/questions/61237131/how-to-make-attribute-in-dataclass-read-only) – norok2 Nov 25 '22 at 09:04
  • Not really, because I am really unsure which solution really works and has not weird sideeffects.... – Gabriel Nov 25 '22 at 09:16
  • 1
    I'd suggest elaborating on what you feel it is different between the two questions, perhaps quoting that question too. I recon that the question does not have a satisfying answer, but perhaps adding a bounty to that question is probably the best way of drawing more attention to it. – norok2 Nov 25 '22 at 14:36

0 Answers0