C.L. wrote: > I was looking for a function or method that would return the index to the > first > matching element in a list. Coming from a C++ STL background, I thought it > might > be called "find". My first stop was the Sequence Types page of the Library > Reference (http://docs.python.org/lib/typesseq.html); it wasn't there. A > search > of the Library Reference's index seemed to confirm that the function did not > exist. A little later I realized it might be called "index" instead. Voila. > > My point is that the docs list and describe it as a method that only exists > for > MUTABLE sequences. Why only for mutables? The class of objects I would expect > it > to cover would be all ordered sequences, or, to phrase it a little more > pointedly, anything that supports ordered INDEXing. My understanding is that > dict's don't fall into that class of objects since their ordering is not > documented or to be depended on. However, tuple's do support ordered indexing, > so why don't tuple's have an index method? > > P.S.: I know I haven't yet gotten an answer to my "why" question yet, but, > assuming it's just an oversight or an example of design without the big > picture > in mind, an added benefit to fixing that oversight would be that the "index" > method's documentation could be moved from the currently odd seeming location > on > the "Mutable Sequence Types" page to a place someone would look for it > logically. > > P.P.S.: As much as the elementary nature of my question would make it seem, > this > isn't my first day using Python. I've used it on and off for several years > and I > LOVE Python. It is only because of my love for the language that I question > its > ways, so please don't be overly defensive when I guess that the cause for this > possible oversight is a lack of design. > > Corey Lubin
Looking around google a little bit, people have been asking that same questions since at least 1992. Here is what the BDFL has to say: Guido van Rossum ([EMAIL PROTECTED]) Wed, 04 Dec 91 18:48:34 +0100 >In reply to: Steven D. Majewski: "Why no index for tuples or strings ?" > >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 <[EMAIL PROTECTED]> -- http://mail.python.org/mailman/listinfo/python-list