[Python-Dev] Immutability vs. hashability
Chris Barker
chris.barker at noaa.gov
Mon Feb 12 14:12:37 EST 2018
I don't seem to be getting my own messages back to reply to, but:
yes, of course, dataclasses won't hash if a field is mutable:
In [*58*]: @dataclasses.dataclass(hash=*True*, frozen=*True*)
...: *class* *Hash*:
...: x: int = 5
...: l: list = dataclasses.field(default_factory=list)
...:
...:
...:
...:
In [*59*]: h = Hash()
In [*60*]: hash(h)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-60-ad376ae4487e> in <module>()
----> 1 hash(h)
/Users/chris.barker/miniconda2/envs/py3/lib/python3.6/site-packages/dataclasses.py
in __hash__(self)
TypeError: unhashable type: 'list'
And the hash does depend on the values of the fields.So you can REALLY
ignore my previous note.
Again, sorry for the noise.
-CHB
On Mon, Feb 5, 2018 at 3:37 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> On Sun, Feb 04, 2018 at 09:18:25PM -0800, Guido van Rossum wrote:
>
> > The way I think of it generally is that immutability is a property of
> > types, while hashability is a property of values.
>
> That's a great way to look at it, thanks.
>
>
> --
> Steve
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: https://mail.python.org/mailman/options/python-dev/
> chris.barker%40noaa.gov
>
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20180212/855fe8ba/attachment-0001.html>
More information about the Python-Dev
mailing list