On 6 February 2018 at 03:47, Guido van Rossum <gu...@python.org> wrote:
> If there's going to be an API for it, it should be in the class, not
> something that mutates the class afterwards.

Something I realised after posting the __class__ setting idea is that
you can actually use a comparable trick to inject an unsafe hash from
the frozen version into the mutable version:

    >>> from dataclasses import dataclass
    >>> @dataclass
    ... class Example:
    ...     a: int
    ...     b: int
    ...
    >>> c = Example(1, 2)
    >>> hash(c)
    Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
    TypeError: unhashable type: 'Example'

    >>> @dataclass(frozen=True)
    ... class LockedExample(Example):
    ...     pass
    ...
    >>> Example.__hash__ = LockedExample.__hash__
    >>> hash(c)
    3713081631934410656

So "unsafe_hash=True" would just be a shorthand spelling of that which
skips creating the full frozen version of the class (and with the
explicit parameter, we can better document the risks of making
something hashable without also freezing it post-creation).

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