[Python-Dev] Dataclasses, frozen and __post_init__
Guido van Rossum
guido at python.org
Sat Feb 17 00:40:01 EST 2018
On Fri, Feb 16, 2018 at 3:38 PM, Ben Lewis <benlewisj at gmail.com> wrote:
> I have been using dataclasses package in a pet project of mine. I'm sorry
> if this issue has already been raised. I came across a situation where I
> wanted to use the __post_init__ function to initialise some inherited
> fields from a dataclass with frozen=True. The problem is that because it is
> frozen, assigning to the field doesn't work.
> There are two workarounds without changing the base class to frozen=False,
> which could be in a library.
> 1. Use object.__setattr__, this is ugly and not very user or beginner
> 2. Extract __post_init__ out into a factory function. Then it also loses
> all the advantages of the __post_init__ and InitVar mechanism.
> Both frozen and unfrozen dataclasses should be able to use the same
> initialisation mechanism for consistency. Being consistent would ease of
> converting an unfrozen dataclass to a frozen one if the only code that
> actually modifies the instance is in __post_init__ function.
> I think frozen classes should be able to be mutated during the
> __post_init__ call. To implements this a frozen dataclass could have a flag
> to says it's not yet fully initialised and the flag would be checked in the
> frozen setattr/delattr methods. This flag could be located as a special
> attribute on the instance or be in a weak reference dict.
That's a pretty tricky proposal, and one that's been debated on and off for
a long time in other contexts. And that flag would somehow have to be part
of every instance's state.
In general the right way to initialize an immutable/frozen object is not
through __init__ but through __new__ -- have you tried that?
Also, a small example that demonstrates your need would do wonders to help
us understand your use case better.
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-Dev