[Python-ideas] What are the strong use cases for str.rindex()?
Guido van Rossum
guido at python.org
Tue Apr 23 16:18:59 EDT 2019
On Tue, Apr 23, 2019 at 1:02 PM MRAB <python at mrabarnett.plus.com> wrote:
> On 2019-04-23 18:52, Terry Reedy wrote:
> > On 4/23/2019 2:44 AM, 林自均 wrote:
> >> Hi all,
> >> I found that there are str.index() and str.rindex(), but there is only
> >> list.index() and no list.rindex().
> > str.index and list.index are related but not the same. The consistency
> > argument is better applied to find-rfind, index-rindex,
> > partition-rpartition, etc.
> > It is much more common to process strings right to left or in both
> > directions, than process lists right to left or in both directions.
> > Moreover, lists have a reverse method, strings do not.
> > ''.join(reversed(somestring)) is likely slower, especially if there many
> > non-ascii chars. Moreover, somestring.rindex(substring) would have to
> > have both somestring and substring reversed when substring is more than
> > one char.
> You can reverse a string with somestring[::-1].
> Personally, I'm not convinced by the "lists can be reversed" argument.
Me neither, though for substring checks, reversing the string would be even
more cumbersome (you'd have to reverse the query string too).
My money is on "nobody uses this for lists".
Some use cases for rindex() on strings that I found in a large codebase
here include searching a pathname for the final slash, a list of
comma-separated items for the last comma, a fully-qualified module name for
the last period, and some ad-hoc parsing of other things. The "last
separator" use cases are the most common and here rindex() sounds very
--Guido van Rossum (python.org/~guido)
*Pronouns: he/him/his **(why is my pronoun here?)*
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas