[Python-ideas] Why can't you put mutable values in a set? (was: Fwd: Why do equality tests between OrderedDict keys/values views behave not as expected?)

Ben Finney ben+python at benfinney.id.au
Sat Dec 19 03:16:22 EST 2015


Guido van Rossum <guido at python.org> writes:

> The link between hashing and immutability is because objects whose
> hash would change are common, e.g. lists, and using them as dict keys
> would be very hard to debug for users most likely to make this
> mistake. […]
>
> […] But the real question here isn't "why aren't all things hashable"
> but "why can't you put mutable values into a set". […]
>
> Hashing comes into play because all of Python's common data structures
> use hashing to optimize lookup -- but if we used a different data
> structure, e.g. something based on sorting the keys, we'd still have
> the mutability problem. And we'd have worse problems, because values
> would have to be sortable, which is a stricter condition than being
> immutable.
>
> In any case, you can't solve this problem by making all values hashable.

That was a great explanation; you answered several points on which I was
vague, and you addressed some things I didn't even know were problems.

I'd love to see that edited to a blog post we can reference in a single
article, if you have the time.

-- 
 \         “I went to the museum where they had all the heads and arms |
  `\      from the statues that are in all the other museums.” —Steven |
_o__)                                                           Wright |
Ben Finney



More information about the Python-ideas mailing list