[Python-ideas] What are the strong use cases for str.rindex()? (John Lin)

Thautwarm Zhao yaoxiansamma at gmail.com
Tue Apr 23 13:50:48 EDT 2019


IMO, there're lots of use cases in parsing related stuffs, which requires
rindex a lot, say, when you have generated a tokenizer which might across
multiple lines:

line 8:   X """
line 9:
line 10:   """

In this case, we need to extract 2 tokens X and , a multiline whitespace
string. After getting each token we're to compute/update the current column
and line number. If the line number gets advanced then we use rindex('\n')
to help with updating the new column number, otherwise,  col_offset +=
len(current_token) .

However, the reason why we don't need list.rindex but do for str.rindex is
simple I'd say: str is immutable and has no O(1) reverse method.

On the other hand, when it comes to list, you can use list.index after
list.reverse, and after a bunch of operations you can resume the state by
invoking list.reverse again.


On Wed, Apr 24, 2019, 12:11 AM <python-ideas-request at python.org wrote:

> Send Python-ideas mailing list submissions to
>         python-ideas at python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>         https://mail.python.org/mailman/listinfo/python-ideas
> or, via email, send a message with subject or body 'help' to
>         python-ideas-request at python.org
>
> You can reach the person managing the list at
>         python-ideas-owner at python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Python-ideas digest..."
> Today's Topics:
>
>    1. What are the strong use cases for str.rindex()? (???)
>
>
>
> ---------- Forwarded message ----------
> From: "林自均" <johnlinp at gmail.com>
> To: python-ideas at python.org
> Cc:
> Bcc:
> Date: Tue, 23 Apr 2019 14:44:25 +0800
> Subject: [Python-ideas] What are the strong use cases for str.rindex()?
> Hi all,
>
> I found that there are str.index() and str.rindex(), but there is only
> list.index() and no list.rindex(). So I filed the issue
> https://bugs.python.org/issue36639 to provide list.rindex(). However, the
> issue was rejected and closed with the comment:
>
> > There were known, strong use cases for str.rindex().  The list.rindex()
> method was intentionally omitted.  AFAICT no compelling use cases have
> arisen, so we should continue to leave it out.  In general, we don't grow
> the core APIs unnecessarily.
>
> However, I am not sure what the known, strong use cases for str.rindex()
> are. Why doesn't the strong use cases apply on list.rindex()? Could anyone
> give me some examples? Thanks.
>
> Best,
> John Lin
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190424/400ede51/attachment.html>


More information about the Python-ideas mailing list