1.

What are iterators in Python?

Answer»
  • An iterator is an object.
  • It remembers its state i.e., where it is during iteration (SEE code below to see how)
  • __iter__() method initializes an iterator.
  • It has a __next__() method which returns the next item in iteration and points to the next element. Upon reaching the end of iterable object __next__() must return StopIteration exception.
  • It is also self-iterable.
  • Iterators are OBJECTS with which we can iterate over iterable objects LIKE lists, strings, etc.
class ArrayList: def __init__(self, number_list): self.numbers = number_list def __iter__(self): self.pos = 0 return self def __next__(self): if(self.pos < len(self.numbers)): self.pos += 1 return self.numbers[self.pos - 1] ELSE: raise StopIterationarray_obj = ArrayList([1, 2, 3])it = iter(array_obj)print(next(it)) #output: 2print(next(it)) #output: 3print(next(it))#Throws Exception#Traceback (most RECENT call last):#...#StopIteration


Discussion

No Comment Found