Re: Why no index for tuples or strings ?

Guido van Rossum (Guido.van.Rossum@cwi.nl)
Wed, 04 Dec 91 18:48:34 +0100

>Most of the functions that operate on mutable sequences but NOT on
>immutable ones are obviously there because they DO CHANGE the sequence.
>BUT: why no string.index() or tuple.index() ?
>
>Is this just an oversight ?
>If not, what is the reason?

Umm, there isn't a real good reason. One thing I can say in my
defense is that string and tuple objects have no methods at all, all
operations on these are done with built-in operations like "+" and
"[...]", so adding an "index" method would be a bit of a change in the
structure.

For tuples, I suspect such a function would rarely be used; I think
that is most cases where x.index() would be useful, x is generally a
list, whose contents varies in time, rather than a tuple (which cannot
change easily).

For strings, there is a built-in module "string" which exports a
function "index" which searches for substrings, so you can say

string.index('one two three', 'two')

--Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
"Oh my god, he's fallen off the edge of the cartoon."