preferring [] or () in list of error codes?

Ben Finney ben+python at
Mon Jun 8 19:43:45 EDT 2009

mh at writes:

> Is there any reason to prefer one or the other of these statements?
>         if e.message.code in [25401,25402,25408]:
>         if e.message.code in (25401,25402,25408):
> I'm currently using [], but only coz I think it's prettier
> than ().

Use a list when the semantic meaning of an item doesn't depend on all
the other items: it's “only” a collection of values.

Your list of message codes is a good example: if a value appears at
index 3, that doesn't make it mean something different from the same
value appearing at index 2.

Use a tuple when the semantic meaning of the items are bound together,
and it makes more sense to speak of all the items as a single structured

The classic examples are point coordinates and timestamps: rather than a
collection of values, it makes more sense to think of each coordinate
set or timestamp as a single complex value. The value 7 appearing at
index 2 would have a completely different meaning from the value 7
appearing at index 3.

James Tauber explains this at

 \       “Pinky, are you pondering what I'm pondering?” “Well, I think |
  `\              so, Brain, but pantyhose are so uncomfortable in the |
_o__)                              summertime.” —_Pinky and The Brain_ |
Ben Finney

More information about the Python-list mailing list