Can some plz explain this code to me | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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]

28th Sep 2017, 1:57 PM
Paavan Gupta
Paavan Gupta - avatar
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
28th Sep 2017, 3:06 PM
Oma Falk
Oma Falk - avatar