Skip to content Skip to sidebar Skip to footer

Ipython, Mutable Dict Keys, Possible Insanity

Dict-keys should probably be immutable, but that's not actually a requirement. All that's required is that the key can be hashed. What happens if I change the object in such a way

Solution 1:

For the sake of completeness, I'll put an answer here so we can close this loop:

The beginning of my question said

"Dict-keys should probably be immutable, but that's not actually a requirement. All that's required is that the key can be hashed."

But, as pointed out by @Stefan Pochmann, that's not precise enough to be accurate. The dict-key must be hashable and the hash should never change over the object's lifetime.

That doesn't mean the object must be immutable, but it does mean the parts of the object that feed into the hash shouldn't change in such a way that they'd change the output of the __hash__() call.

So, when I altered my instance in such a way as to change the hash, I violated the requirement and all bets are off - I can no longer make expectations about how this object will act. The "issue" with IPython then is just a matter of it making valid assumptions about objects that I've violated (the assumptions, not the objects), so it's perfectly understandable for it to error out on me.

Post a Comment for "Ipython, Mutable Dict Keys, Possible Insanity"