On 20 February 2018 at 15:11, Steven D'Aprano <st...@pearwood.info> wrote:
> 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.

This doesn't technically require any additional changes, as you can
already do it with __post_init__:

    @dataclass
    class MyRecord:
        a: int
        b: str
        c: float

        def __post_init__(self):
            # self is still mutable here
            self.__class__ = _LockedMyRecord

    @dataclass(frozen=True)
    class _LockedMyRecord(MyRecord):
        pass

This is also the kind of runtime behaviour modification where hiding
how it's implemented is likely to create more problems than it solves,
as even though having "type(obj)" and "obj.__class__" refer to
different types is a formally supported state for instances, there are
also lots of APIs where it isn't well defined whether an attribute
lookup will use the nominal class or the true underlying type.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncogh...@gmail.com   |   Brisbane, Australia
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to