Nov-10-2019, 04:21 PM
(This post was last modified: Nov-10-2019, 04:21 PM by Gribouillis.)
I would define instead a class that obeys the iterator protocol, that is to say it has
__iter__()
and __next__()
methods and the latter raises StopIteration when the iterator is exhausted. For exampleclass WalkList: def __init__(self, seq): if not isinstance(seq, list): raise TypeError('list argument expected, got', type(seq)) self.seq = seq self.reset() def reset(self): self.ite = iter(self.seq) def __next__(self): return next(self.ite) def __iter__(self): return self walk = WalkList(list(range(7))) r = True for item in walk: if r and item == 3: walk.reset() r = False continue print(item)
Output:λ python3 paillasse/genstate.py
0
1
2
0
1
2
3
4
5
6