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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s