Python: mutability is the key
April 28, 2008
I’ve been coding in Python since 2000, so I surprised myself by discovering a gap in my coding knowledge this morning. Reading the docs on sets, I came across: “The set type is mutable — the contents can be changed using methods like add() and remove(). Since it is mutable, it has no hash value and cannot be used as either a dictionary key or as an element of another set.”
I’d never realized that mutability decides whether an object can be a key. I guess I always just coded that way, with my keys generally being ints or strings, or tuples thereof.