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

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Tue Jun 9 04:02:33 EDT 2009


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. 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. Prohibiting the use of lists in the context 
of ordered data is surely is an unfortunate consequence of your advice.


> James Tauber explains this at
> <URL:http://jtauber.com/blog/2006/04/15/
> python_tuples_are_not_just_constant_lists/>.


He doesn't really explain anything though, he merely states it as 
revealed wisdom. The closest he comes to an explanation is to declare 
that in tuples "the index in a tuple has an implied semantic. The point 
of a tuple is that the i-th slot means something specific. In other 
words, it's a index-based (rather than name based) datastructure." But he 
gives no reason for why we should accept that as true for tuples but not 
lists.

It may be that that's precisely the motivation Guido had when he 
introduced tuples into Python, but why should we not overload tuples with 
more meanings than Guido (hypothetically) imagined? In other words, why 
*shouldn't* we treat tuples as immutable lists, if that helps us solve a 
problem effectively?

To put it another way, I think the question of whether or not tuples are 
immutable lists has the answer Mu. Sometimes they are, sometimes they're 
not. I have no problem with the title of the quoted blog post -- that 
tuples are not *just* constant lists -- but I do dispute that there is 
any reason for declaring that tuples must not be used as constant lists. 
As tuples are defined in Python, they quack like immutable lists, they 
walk like immutable lists, and they swim like immutable lists. Why 
shouldn't we treat them as immutable lists?

Phillip Eby states that "Lists are intended to be homogeneous sequences, 
while tuples are heterogeneous data structures." (Notice the subtle shift 
there: lists are "intended", while tuples "are". But in fact, there's 
nothing to stop you from putting homogeneous data into a tuple, so Eby is 
wrong to say that tuples *are* heterogeneous.)

Perhaps Eby intends lists to be homogeneous, perhaps Guido does too, but 
this is Python, where we vigorously defend the right to shoot ourselves 
in the foot. We strongly discourage class creators from trying to enforce 
their intentions by using private attributes, and even when we allow such 
a thing, the nature of Python is that nothing is truly private. Why 
should homogeneity and heterogeneity of lists and tuples be sacrosanct? 
Nothing stops me from putting hetereogeneous data into a list, or 
homogeneous data into a tuple, and there doesn't appear to be any ill-
effects from doing so. Why give lose sleep over the alleged lack of 
purity?



-- 
Steven



More information about the Python-list mailing list