7.7 Iterators

The difference between Iterables, Iterators and Generators.

  1. >Iterables: Are objects that can be iterated, they can be iterated as many times as required. They need to implement the __iter__() method.
  2. >Iterators: Are also iterable but are "lazy" and can be iterated only once, they need to be re-created for iterating again. They need to implement __iter__() and __next__() methods. They are used when it is required to have iterator functionality inside a complex class (with other functionalities).
  3. >Generators: Are iterable and iterator (but not vice versa). Generators are an easy way to create an iterator object. They can be created using a function with a yield statement or using generator comprehension. They can be used when we need a standalone iterator object.