Op 26-11-15 om 09:27 schreef Marko Rauhamaa: > Chris Angelico <ros...@gmail.com>: > >> On Thu, Nov 26, 2015 at 5:52 PM, Marko Rauhamaa <ma...@pacujo.net> wrote: >>> Nothing prevents using mutable objects as keys in Python. >> Sure, you _can_. But if the key's hash changes between dict insertion >> and retrieval, all manner of invariants will break, and likewise if >> two equal objects have different hashes. From which you can deduce >> logically that any object used as a key must remain (not) equal to >> everything that it was (not) equal to from that time until it is >> looked up... which basically means its value mustn't change. It must >> be immutable. > What I'm saying is that Python does not prevent mutable keys but tries > to do that with lists and tuples. > > I think Python should stop trying. > > I have wanted to use lists as keys, and there should be no reason to > allow mutable tuples. It should be enough to say that the behavior of a > dictionary is undefined if a key should mutate on the fly.
Maybe python could introduce a dictionary that copies the keys in and out the dictionary. In that case keys can be mutable without that being a problem for the dictionary, because the dictionary works with it's own copy of the key that it doesn't mutate. -- Antoon. -- https://mail.python.org/mailman/listinfo/python-list