[Python-Dev] List vs Tuple / Homogeneous vs Heterogeneous / Mutable vs Immutable

Raymond Hettinger raymond.hettinger at gmail.com
Tue Apr 22 08:39:13 CEST 2014


On Apr 18, 2014, at 1:21 AM, Jeff Allen <ja.py at farowl.co.uk> wrote:

> The "think of tuples like a struct in C" explanation immediately reminded me that ...
> 
> On 16/04/2014 21:42, Taavi Burns wrote (in his excellent notes from the language summit):
>>  The demographics have changed. How do
>> we change the docs and ecosystem to avoid the assumption that Python
>> programmers already know how to program in C? 

In teaching Python, I find that analogs to other languages are helpful
in explaining Python even if a person doesn't know the other language.

    sorted(set(open(somefile)))

is like:

    cat somefile | sort | uniq       # different algorithm, same outcome

or:

   SELECT DISTINCT line FROM somelines ORDER BY line;

In particular, it is effective to say that tuples are used like structs in other languages
or like records in database tables.

I don't think we should go down the road of removing all the similes and metaphors
from the docs.   While children don't usually benefit from them, adults face different
learning challenges.  Usually, their biggest learning hurdle is figuring-out what is
and what is not a valid comparison to other things they already know.

One problem I have seen with the usual list vs tuple explanations is that
they seem to suggest that the list-are-for-looping and tuples-are-like-records
notions are baked into the implementation of lists and tuples.

In fact, the distinction is extrinsic to their implementations.  It is only important
because the rest of the language tends to treat them differently.  For example,
you could store ['raymond', 'red'] as a list or as a tuple ('raymond', 'red'), but you
wouldn't be punished until later when you tried:

     'I think %s likes %s' % container     # str.__mod__ treats lists and tuples differently

Likewise, there seems to be wide-spread confusion about make makes an
object immutable.  People seem to miss that ints, tuples, None and str are
immutable only because they lack any mutating methods,


Raymond


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20140421/a83dbf26/attachment.html>


More information about the Python-Dev mailing list