tuples, index method, Python's design

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


On 2007-04-11, Marc 'BlackJack' Rintsch <bj_666 at gmx.net> wrote:
> In <slrnf1pnng.klv.apardon at rcpc42.vub.ac.be>, Antoon Pardon wrote:
>
>> On 2007-04-11, Steven D'Aprano <steve at REMOVE.THIS.cybersource.com.au> wrote:
>>> 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.
>
> Then you are confused.  The typical use case for tuples are database
> records.  The columns in the table can have completely different types but
> the values in a row, represented as a Python tuple, of course belong
> together.

Don't blame me. I don't agree with the view. But that was sort of the
explanation that was given here last time I remember this topic came 
up in defending why tuples and lists differ in a number of ways that
are less obvious.

They wrote about lists containing homogeneous items and tuples
containing hetergenous items but stressed rather strongly that
this shouldn't be understood in terms of type similarities.

> The homogeneous objects in lists must not be of the same type but share
> some behavior so it makes sense to apply some operation on all the
> elements.  For example get the length of each item or sum them all up.

No they don't. The counter example is using a list as a stack when
evaluating expressions. You can use one stack to store the still
to be treated numbers and operands and those two don't need
common behaviour.

-- 
Antoon Pardon



More information about the Python-list mailing list