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

Gabriel Genellina gagsl-py2 at
Thu Jun 11 16:47:58 CEST 2009

En Tue, 09 Jun 2009 05:02:33 -0300, Steven D'Aprano
<steven at> escribió:

> [...] 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?

Yes - but in the past the distinction was very much stronger. I think that
tuples didn't have *any* method until Python 2.0 -- so, even if someone
could consider a tuple a "read-only list", the illusion disappeared as
soon as she tried to write anything more complex that a[i]. Maybe tuples
could quack like immutable lists, but they could not swim nor walk...

With time, tuples gained more and more methods and are now very similar to
lists - they even have an index() method (undocumented but obvious) which
is absurd in the original context. Think of tuples as used in relational
databases: there is no way in SQL to express the condition "search for
this along all values in this tuple", because it usually doesn't make any
sense at all (and probably, if it does make sense in a certain case, it's
because the database is badly designed.)

But *now*, you can express that operation in Python. So I'd say that
*now*, the distinction between an "homogeneous container" vs
"heterogeneous data structure" has vanished a lot, and it's hard to
convince people that tuples aren't just immutable lists. That is, *I*
would have used a list in this case:

for delay in (0.01, 0.1, 0.5, 1, 2, 5, 10, 30, 60):

but I cannot find a *concrete* reason to support the assertion "list is

So, for practical purposes, tuples act now as if they were immutable lists
-- one should be aware of the different memory allocation strategies, but
I see no other relevant differences.

Gabriel Genellina

More information about the Python-list mailing list