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

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@python.org wrote:

On Wed, Apr 24, 2019 at 01:50:48AM +0800, Thautwarm Zhao wrote:
list reverse is not O(1), and flipping the order, then flipping the order back again is not safe if the list could be accessed by two or more threads. (The call to reverse itself is thread-safe, but not the operations in between.) -- Steven

On Wed, Apr 24, 2019 at 01:50:48AM +0800, Thautwarm Zhao wrote:
list reverse is not O(1), and flipping the order, then flipping the order back again is not safe if the list could be accessed by two or more threads. (The call to reverse itself is thread-safe, but not the operations in between.) -- Steven
participants (2)
-
Steven D'Aprano
-
Thautwarm Zhao