0
Can some plz explain this code to me
class Alphabet(): def __init__(self, letters): self.letters = letters self.index = -1 def __iter__(self): return (self) def __next__(self): if(self.index) >= len(self.letters)-1 : raise StopIteration self.index += 1 return self.letters[self.index]
1 Answer
+ 1
Alphabet is an iterable object. So it implements the methods __iter__ and __next__.
__iter__ has to return an iterable object which means an object with a method __next__.
Normally this object is the object itself.
The method __next__ describes, how to iterate the object. Here you are free.
see example for different iterators:
https://code.sololearn.com/c7LSgVH4CgxZ/?ref=app