tuples, index method, Python's design

Alan Isaac aisaac at american.edu
Thu Apr 12 17:56:48 EDT 2007


Chris Mellon said:
> Sure. I have never done this. In fact, I have only ever written code
> that converted a tuple to a list once, and it was because I wanted
> pop(), not index()

Well then you apparently made a *mistake*: you chose a tuple when you
wanted a mutable object.  That is really beside the point.

And you missed the point of my query.  It is not that existing code will
contain such a conversion to get access to the index method.  It is that
if you choose tuples to represent immutable sequences, sooner or later
you will find you need to change your code to use a list not because you
really want a mutable sequence but because you want the ``index`` method.

Note that it has become clear that some people do not use tuples hardly
ever, regardless whether their sequence is naturally mutable or
immutable.  Why?  Because they want access the the list methods.
**All** of these people fall in the category I am talking about.
I do not really care if you reach mentally and then fix or actually type it.
The avoidance of tuples, so carefully defended in other terms,
is often rooted (I claim) in habits formed from need for list methods like
``index`` and ``count``.  Indeed, I predict that Python tuples
will eventually have these methods and that these same people
will then defend *that* status quo.

Bottom line:
I do not consider being forced to disregard the natural mutability or
immutability of an object as a "good thing".  If you do not like
my toy example, look at some of the others that have been offered.
Getting information about point tuples has been my favorite so far.

Btw, the tendency in this debate to ask if a tuple is "necessary" is
both a red herring and obvious evidence of thoughtlessness.  How many
coding practices are "necessary"?  The question is just whether
we will be allowed to treat sequences that are naturally immutable
as immutable or will be forced for no real reason (I believe the
"code maintenance" issue has been dealt with) to treat them
differently.

Cheers,
Alan Isaac






More information about the Python-list mailing list