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

Gunter Henriksen gunterhenriksen at gmail.com
Thu Jun 11 19:34:56 EDT 2009


> 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.

I totally agree about anything to do with immutability,
I think the relative ordering of the elements in this
example may be orthogonal to the concept of a tuple
as an object whose elements have a semantic meaning
implicitly defined by location in the sequence... in
other words knowing that element i+1 is in some sense
ordinally smaller than element i does not give me much
information about what element i+1 actually is.

To me a timestamp could be (date, time), or (days,
seconds, microseconds) (as in datetime.timedelta()), so
it is not clear to me that using a tuple as something
where the semantic meaning of the element at position i
should readily apparent would be the best approach
for timestamps, or enough to distinguish list and tuple
(in other words I am not suggesting a dict or class).

In the case of something like (x, y) or (real, imag),
or (longitude, latitude), or any case where there is
common agreement and understanding, such that using
names is arguably superfluous... I think in those
cases the concept makes sense of a tuple as a sequence
of attributes whose elements have a semantic meaning
implicitly defined by position in the sequence.  My
feeling is the number of cases where tuples are better
than lists for that is small relative to the number of
cases where tuple adds value as an immutable list.

I do not mean to be suggesting that a tuple should only
ever be used or thought of as a "frozenlist" though.



More information about the Python-list mailing list