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

Ben Finney ben+python at benfinney.id.au
Thu Jun 11 17:55:24 EDT 2009


Gunter Henriksen <gunterhenriksen at gmail.com> writes:

> I think I would have difficulty holding a position that this should
> not be a class (or equivalent via namedtuple()) or a dict. It seems to
> me like a case could be made that there are far more situations where
> it makes sense to use tuples as immutable sequences than as objects
> whose attributes are named implicitly by an index. This dodge_city
> definitely does not seem to me like a good candidate for a plain
> tuple.

It's a fair cop. (I only meant that for this example a tuple was
superior to a list, but you're right that a dict would be better than
either.)

Try, then, this tuple:

    event_timestamp = (2009, 06, 04, 05, 02, 03)
    (year, month, day, hour, minute, second) = event_timestamp

A list would be wrong for this value, because each position in the
sequence has a specific meaning beyond its mere sequential position. Yet
it also matters to the reader that these items are in a specific
sequence, since that's a fairly standard ordering for those items.

In this case, a tuple is superior to a list because it correctly conveys
the semantic meaning of the overall value: the items must retain their
sequential order to have the intended meaning, and to alter any one of
them is conceptually to create a new timestamp value.

-- 
 \     “[The RIAA] have the patience to keep stomping. They're playing |
  `\         whack-a-mole with an infinite supply of tokens.” —kennon, |
_o__)                                             http://kuro5hin.org/ |
Ben Finney



More information about the Python-list mailing list