tuples, index method, Python's design

Antoon Pardon apardon at forel.vub.ac.be
Wed Apr 11 09:13:20 EDT 2007


On 2007-04-11, Steven D'Aprano <steve at REMOVE.THIS.cybersource.com.au> wrote:
> On Wed, 11 Apr 2007 08:57:43 +0200, Hendrik van Rooyen wrote:
>
>> I can write:
>> 
>> L = [a,b,c,d,e,f]
>> T= (a,b,c,d,e,f)
>> 
>> The difference between the two things is that I can add to 
>> and change L, but not T.
>
> No, that's *one* difference between the two things. There are other
> differences, e.g. the obvious is that they are different types (and
> therefore different string representations), but also some not so obvious:
>
>>>> hash((1,2,3))
> -378539185
>
>>>> hash([1,2,3])
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: list objects are unhashable
>
>
>
>> Now it seems to me that whatever argument is used to justify the
>> existence of:
>> 
>> n = L.index(d)
>> 
>> can be used to justify the existence of:
>> 
>> n = T.index(d)
>> 
>> and vice versa.
>
> That depends on what you the purpose of lists and tuples are. Yes, they
> are both sequences, but they have different uses. Ball-peen hammers and
> tack hammers are both hammers, but they have different purposes and
> therefore different functionality. Likewise for tuples and lists:
>
> Lists are designed for sequences of homogeneous items, e.g.:
>
> L = [1, 2, 4, 8, 16, 32]
> while tuples are designed to be more like structs or records, with
> heterogeneous items, e.g.:
>
> T = ("Fred", 32, 12.789, {}, None, '\t')

I think you are confused. Last time I heard this homogeneous items stuf,
it had nothing to do with the types being the same. They were homogeneous
because they somehow belonged together and heterogeneous because they
just happened to live together. Similarity of type played no part in
calling the data homogeneous or heterogeneous.

-- 
Antoon Pardon



More information about the Python-list mailing list