[Python-Dev] Dataclasses, frozen and __post_init__
Steven D'Aprano
steve at pearwood.info
Tue Feb 20 00:11:47 EST 2018
On Mon, Feb 19, 2018 at 07:02:42PM -0800, Guido van Rossum wrote:
> On Mon, Feb 19, 2018 at 5:06 PM, Chris Barker - NOAA Federal <
> chris.barker at noaa.gov> wrote:
[...]
> > This seems another use case — maybe it would be helpful to be able to
> > freeze an instance after creation for multiple use-cases?
>
> But how?
I haven't got all the details yet, but how badly do you hate the
technique of swapping out __class__ to get a change in behaviour?
https://www.safaribooksonline.com/library/view/python-cookbook/0596001673/ch05s19.html
or https://code.activestate.com/recipes/68429-ring-buffer/
(I believe that in the Objective C community, something very close to
this is called "swizzling".)
So in principle, we could have a mutable class, and a immutable one, and
when you flick the switch, the instance.__class__ changes from mutable
to frozen.
If you don't hate this, we can think about the details needed to get
it work in practice.
--
Steve
More information about the Python-Dev
mailing list