tuples, index method, Python's design

Chris Mellon arkanes at gmail.com
Mon Apr 16 12:57:24 EDT 2007


On 4/12/07, Alan Isaac <aisaac at american.edu> wrote:
> 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.
>

The tuple in question was function varargs. Yes, I wanted a mutable
object, thats why I made a list out of it. Whether varargs should be a
list or a tuple to begin with is
open to debate, but it's not likely you'd want to use index() on it either way.

> 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.
>

That may have been your point, but it's not what you said. I've never
converted a tuple to a list because I needed index(). I've never used
a list where a tuple would be "more natural" soley because I needed to
search it, either. I have never used index() in a situation where the
sequence in question wasn't both a) mutable and b) actually was
mutated in the course of program.

> 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.
>

I use tuples all the time. There are more incidences of tuple usage
than list usage in my current codebase. My opinions on tuples still
stand. I use tuples when I already know what goes into them and in
what order. index() and count() are totally useless when you use
tuples (or, indeed, any sequence) in this manner.



More information about the Python-list mailing list