Help understanding the decisions *behind* python?

Raymond Hettinger python at rcn.com
Fri Jul 31 19:49:04 CEST 2009


On Jul 20, 9:27 am, Phillip B Oldham <phillip.old... at gmail.com> wrote:
> Specifically the "differences" between lists and tuples have us
> confused and have caused many "discussions" in the office. We
> understand that lists are mutable and tuples are not, but we're a
> little lost as to why the two were kept separate from the start. They
> both perform a very similar job as far as we can tell.

The underlying C code for the two is substantially the same.  In terms
of code, tuples are in effect just immutable lists that have the
added
feature of being hashable (and therefore usable as dictionary keys or
elements of sets).

Beyond the mutable/hashable distinction, there is an important
philosophical distinction articulated by Guido.  He deems tuples
to be useful for struct like groupings of non-homogenous fields
and lists to be useful for sequences of homogenous data suitable
for looping.

While nothing in the list/tuple code requires you to make that
distinction,
it is important because that philosophy pervades the language.  If you
follow Guido's direction, you'll find that the various parts of the
language fit together better.  Do otherwise and you'll be going
against
the grain.


Raymond

P.S.  The C code for lists and tuples have a slightly difference
internal
structure that makes tuples a little more space efficient (since their
size is known at the outset and doesn't change during use).



More information about the Python-list mailing list