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

Gunter Henriksen gunterhenriksen at gmail.com
Thu Jun 11 21:14:35 EDT 2009


> > >    event_timestamp = (2009, 06, 04, 05, 02, 03)
> > >    (year, month, day, hour, minute, second) = event_timestamp
> >
> > [...]
>
> The point of each position having a different semantic meaning is that
> tuple unpacking works as above. You need to know the meaning of each
> position in order to unpack it to separate names, as above.
>
> So two tuples that differ only in the sequence of their items are
> different in meaning. This is unlike a list, where the sequence of items
> does *not* affect the semantic meaning of each item.

I do not feel the above is significantly different enough from

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

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

    event_timestamp = [2009, 06, 04, 05, 02, 03]
    [year, month, day, hour, minute, second] = event_timestamp

to suggest tuples are really adding significant value
in this case, especially when I can do something like

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

and not have any indication I have done the wrong thing.

I guess to me, fundamentally, the interpretation of
tuple as a sequence whose elements have semantic meaning
implicitly defined by position is a relatively abstract
intrepretation whose value is dubious relative to the
value of immutability, since it seems like a shortcut
which sacrifices explicitness for the sake of brevity.

I would feel differently if seemed unusual to find good
Python code which iterates through the elements of a
tuple as a variable length homogenous ordered collection.
But then I would be wishing for immutable lists...



More information about the Python-list mailing list