[Python-il] The Tuples and Lists question
Beni Cherniavsky
cben at users.sf.net
Wed Jun 11 23:44:56 IDT 2008
oops, forgot to reply to all.
2008/6/11, Omer Zak <w1 at zak.co.il>:
> I have a silly question:
> Why does Python have both Tuples and Lists?
>
The short answer is: because Guido likes it :-)
I know of 3 reasons:
1. hashability
2. expressibilty
3. historical reasons
It's also worth noting that there is actually very little harm in the
distinction.
As long as it gives some benefit, it will stay.
> The only programmer-visible difference, of which I am aware, between
> them is that Tuples are immutable and Lists are mutable, with the
> following implications:
> 1. Tuples can be used as hash (Dict) keys, and Lists cannot.
There is similar redudancy between set and frozenset.
The fact that only immutable types can be hashed seems a petty
technical difficulty, but it's quite hard to get rid of it.
> 2. When you modify a Tuple, a new Tuple is created. On the other hand,
> List modification happens in place.
>
<nit-picking>
Strictly speaking, you never modify a tuple.
You MAY modify a list (``lst.append(1)``), or create a new list
(``lst + [1]``).
Which of these happens is explicit in the code you write (except for
the ``+=`` operator (and friends) that confuse the issue by using the
same syntax for both).
</nit-picking>
> I don't see why is the above difference good enough reason to have
> different notations for Tuples and Lists.
Guido mostly defends the distinction for expressibility reasons.
There is a rule-of-thumb clearly dividing most uses:
* use lists for homogenous data of variable length
* use tuples where you would use structs in C - heterogenous data of
fixed length
(of course it's only good for small lightweight structs; at some
point a class with named attributes becomes better).
An alternative rule of thumb is about use:
* use lists if you expect to randomly access individual elements
* use tuples if you usually expect to consider or replace the whole thing
Turns out that these two rules usually give the same answer, leading
credence to Guido's claim (which is always correct of course ;-) that
we have here 2 different concepts and intents, which are best served
by 2 different types and notations.
3. Appendix: The historical reason:
When Python was very young, functions always got only one argument.
tuples were invented to conveniently pass several argument.
So ``f(x, y)`` was actually equivallent to ``args = x, y; f(args)``
That's why tuples are syntactically formed by commas, and the parens
are optional.
This is a sound mathematical way of looking at multi-argument functions,
but experience showed that it was awkward in practice, and around
Python 0.8 (or something like that) multiple arguments became a
built-in part of the syntax.
But tuples remained...
--
Beni Cherniavsky <cben at users.sf.net> (I read email only on weekends)
--
Beni Cherniavsky <cben at users.sf.net> (I read email only on weekends)
More information about the Python-il
mailing list