/ Design Patterns / Behavioral patterns / Iterator Iterator Design Pattern in Python Back to Iterator description """ Provide a way to access the elements of an aggregate objects equentially without exposing its underlying representation. """ import collections.abc class ConcreteAggregate(collections.abc.Iterable): """ Implement the Iterator creation interface to return an instance of the proper ConcreteIterator. """ def __init__(self): self._data = None def __iter__(self): return ConcreteIterator(self) class ConcreteIterator(collections.abc.Iterator): """ Implement the Iterator interface. """ def __init__(self, concrete_aggregate): self._concrete_aggregate = concrete_aggregate def __next__(self): if True: # if no_elements_to_traverse: raise StopIteration return None # return element def main(): concrete_aggregate = ConcreteAggregate() for _ in concrete_aggregate: pass if __name__ == "__main__": main() Support our free website and own the eBook! 22 design patterns and 8 principles explained in depth 406 well-structured, easy to read, jargon-free pages 228 clear and helpful illustrations and diagrams An archive with code examples in 4 languages All devices supported: EPUB/MOBI/PDF formats Learn more...