On Wednesday 05 November 2003 05:34 pm, Samuele Pedroni wrote: ... > I think he was wondering whether people rely on > > enumerate([1,2]).next > i = enumerate([1,2]) > i is iter(i) > > working , vs. needing iter(enumerate([1,2]).next > > I think he was proposing to implement enumerate as > > class enumerate(object): > def __init__(self,iterable): > self.iterable = iterable > > def __iter__(self): > i = 0 > for x in self.iterable: > yield i,x > i += 1 > > def __reversed__(self): > rev = reversed(self.iterable) > try: > i = len(self.iterable)-1 > except (TypeError,AttributeError): > i = -1 > for x in rev: > yield i,x > i -= 1 Ah, I see -- thanks! Well, in theory you COULD add a 'next' method too: def next(self): self.iterable = iter(self.iterable) try: self.index += 1 except AttributeError: self.index = 0 return self.index, self.iterable.next() (or some reasonable optimization thereof:-) -- now __reversed__ would stop working after any .next call, but that would still be OK for all use cases I can think of. Alex
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4