terminological obscurity

Duncan Booth me at privacy.net
Fri May 28 04:30:54 EDT 2004


Grant Edwards <grante at visi.com> wrote in 
news:slrncb4qc3.84u.grante at grante.rivatek.com:

> On 2004-05-24, Christos TZOTZIOY Georgiou <tzot at sil-tec.gr> wrote:
> 
>> Take a sequence of items.  If there is a point in comparing an item of
>> the sequence to another, use a list.  If not, use a tuple.
>>
>> You can stop reading here.
> 
> Unless you want to use it as a key for a dictionary.  Then use
> a tuple.
> 
>> Of course, you can always use a list instead of a tuple in all cases,
>> with the minor exception of using a tuple when you want to index a
>> mapping.
> 
>:)
> 

You missed a couple of other important cases where you need to use a tuple 
rather than a list, even though the elements are homogenous:

isinstance takes as its second argument: a class, type or a tuple of things 
it takes as its second argument.

try ... except expression, target: suite
The expression must be an exception class (or a string) or a tuple of 
things that can appear as the expression here.

The common factor in both of these cases is that by only allowing tuples 
instead of lists you can allow the expressions to nest to an arbitrary 
degree, but never have to worry about infinite loops caused by cycles in 
the data structure. (Although, stack overflow can still be a problem as 
evidenced by the Python 2.3.4 release notes)




More information about the Python-list mailing list