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

Ben Finney ben+python at benfinney.id.au
Tue Jun 9 08:56:17 EDT 2009


Steven D'Aprano <steven at REMOVE.THIS.cybersource.com.au> writes:

> On Tue, 09 Jun 2009 09:43:45 +1000, Ben Finney wrote:
> 
> > 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.
> 
> That advice would seem to imply that lists shouldn't be ordered.

No such implication. Order is important in a list, it just doesn't
change the semantic meaning of the value.

> If a list of values has an order, it implies that "first place" (index
> 0) is different from "second place", by virtue of the positions they
> appear in the list. The lists:
> 
> presidential_candidates_sorted_by_votes = ['Obama', 'McCain']
> presidential_candidates_sorted_by_votes = ['McCain', 'Obama']
> 
> have very different meanings.

But the semantic meaning if each value is unchanged: each is still a
presidential candidate's surname. The additional semantic meaning of
putting it in a list is no more than the position in the sequence. A
list is the right choice, for that reason.


Whereas, for example, in this tuple:

    dodge_city = (1781, 1870, 1823)
    (population, feet_above_sea_level, establishment_year) = dodge_city

each index in the sequence implies something very different about each
value. The semantic meaning of each index is *more* than just the
position in the sequence; it matters *for interpreting that component*,
and that component would not mean the same thing in a different index
position. A tuple is the right choice, for that reason.

-- 
 \       “Are you pondering what I'm pondering?” “Umm, I think so, Don |
  `\          Cerebro, but, umm, why would Sophia Loren do a musical?” |
_o__)                                           —_Pinky and The Brain_ |
Ben Finney



More information about the Python-list mailing list