A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2018-February/152242.html below:

[Python-Dev] Should the dataclass frozen property apply to subclasses?

[Python-Dev] Should the dataclass frozen property apply to subclasses?Raymond Hettinger raymond.hettinger at gmail.com
Thu Feb 22 01:56:08 EST 2018
When working on the docs for dataclasses, something unexpected came up.  If a dataclass is specified to be frozen, that characteristic is inherited by subclasses which prevents them from assigning additional attributes:

    >>> @dataclass(frozen=True)
    class D:
            x: int = 10

    >>> class S(D):
            pass

    >>> s = S()
    >>> s.cached = True
    Traceback (most recent call last):
      File "<pyshell#49>", line 1, in <module>
        s.cached = True
      File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/dataclasses.py", line 448, in _frozen_setattr
        raise FrozenInstanceError(f'cannot assign to field {name!r}')
    dataclasses.FrozenInstanceError: cannot assign to field 'cached'

Other immutable classes in Python don't behave the same way:


    >>> class T(tuple):
            pass

    >>> t = T([10, 20, 30])
    >>> t.cached = True

    >>> class F(frozenset):
            pass

    >>> f = F([10, 20, 30])
    >>> f.cached = True

    >>> class B(bytes):
            pass

    >>> b = B()
    >>> b.cached = True


Raymond
More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4