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

Terry Reedy tjreedy at udel.edu
Fri Apr 26 00:17:57 EDT 2019


On 4/25/2019 7:12 PM, Greg Ewing wrote:
> Steven D'Aprano wrote:
>> I too often forget that reverse() returns an iterator,

I presume you mean reversed().  list.reverse() is a list

> That seems like a mistake. Shouldn't it return a view?

RL = reversed(somelist) is already partly view-like.  The nth next call 
returns the nth item at the time of the next call, rather than at the 
time of the reversed call.  However, the number of items produced by 
next calls is the length of the list at the time of the reversed call. 
The first next(RL) is the current somelist[captured_length].

 >>> somelist = [1,2,3]
 >>> RL = reversed(somelist)
 >>> somelist[-1] = None
 >>> somelist.append(4)
 >>> list(RL)
[None, 2, 1]



-- 
Terry Jan Reedy



More information about the Python-ideas mailing list