Iterator Pattern Damian Gordon
Iterator Pattern • The iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements.
Iterator Pattern • The iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements.
Iterator Pattern • The iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements.
Iterator Pattern • The iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements.
Iterator Pattern • The iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements.
Iterator Pattern • The iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements.
Iterator Pattern • The iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements.
Iterator Pattern • The iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements.
Iterator Pattern • In a programming language without patterns, an iterator would have a next() method and a done() method, and the iterator loops across all the containers using these methods.
Iterator Pattern • In a programming language without patterns, an iterator would have a next() method and a done() method, and the iterator loops across all the containers using these methods. WHILE NOT(iterator.done()) DO item = iterator.next() # do more stuff ENDWHILE;
Iterator Pattern • The iterator pattern in Python has the following features: Name Description _ _next_ _() The next method returns the next element from the container. StopIteration() The StopIteration is an exception that is raised when the last element is reached. _ _iter_ _() The iter method makes the object iterable, and returns an iterator.
Iterator Pattern • The iterator pattern has two parts: –An iterable class (to create the iterator object) –An iterator class (to traverse a container)
def __init__(self, VALUE): self.VALUE = VALUE # END Init def __iter__(self): return ITERATOR(self.VALUE) # END Iter class ITERABLE: # END ITERABLE.
def __init__(self, VALUE): self.VALUE = VALUE self.index = 0 # END Init def __iter__(self): return self # END Iter def __next__(self): if CONDITION: VALUE = SELF.VALUE self.index = self.index + 1 return VALUE else: raise StopIteration() # ENDIF; # END Next class ITERATOR: # END ITERATOR.
Iterator Pattern • Let’s look at a simple example that counts numbers, there’ll be three parts: – The Iterable part – The Iteration part – The program execution part
Iterator Pattern class MyCountIterable: def __init__(self, Value): self.Value = Value # END Init def __iter__(self): return MyCountIteration(self.Value) # END Iter # END MyCountIterable.
Iterator Pattern class MyCountIteration: def __init__(self, Value): self.Index = 0 self.Value = Value # END Init def __iter__(self): # Iterators are iterables too. return self # END Iter def __next__(self): if self.Index < self.Value: # THEN Index = self.Index self.Index += 1 return Index else: raise StopIteration() # ENDIF; # END Next # END MyCountIteration.
Iterator Pattern FirstCount = MyCountIterable(5) list(FirstCount) FirstCountIter = iter(FirstCount) while True: # DO try: print(next(FirstCountIter)) except StopIteration: break # ENDWHILE
Iterator Pattern FirstCount = MyCountIterable(5) list(FirstCount) FirstCountIter = iter(FirstCount) while True: # DO try: print(next(FirstCountIter)) except StopIteration: break # ENDWHILE 0 1 2 3 4
Iterator Pattern • Hang on… • Couldn’t we just do: • YES! for Counter in range(0,5): print(Counter) # ENDFOR;
etc.

Python: The Iterator Pattern

  • 1.
  • 2.
    Iterator Pattern • Theiterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements.
  • 3.
    Iterator Pattern • Theiterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements.
  • 4.
    Iterator Pattern • Theiterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements.
  • 5.
    Iterator Pattern • Theiterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements.
  • 6.
    Iterator Pattern • Theiterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements.
  • 7.
    Iterator Pattern • Theiterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements.
  • 8.
    Iterator Pattern • Theiterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements.
  • 9.
    Iterator Pattern • Theiterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements.
  • 10.
    Iterator Pattern • Ina programming language without patterns, an iterator would have a next() method and a done() method, and the iterator loops across all the containers using these methods.
  • 11.
    Iterator Pattern • Ina programming language without patterns, an iterator would have a next() method and a done() method, and the iterator loops across all the containers using these methods. WHILE NOT(iterator.done()) DO item = iterator.next() # do more stuff ENDWHILE;
  • 12.
    Iterator Pattern • Theiterator pattern in Python has the following features: Name Description _ _next_ _() The next method returns the next element from the container. StopIteration() The StopIteration is an exception that is raised when the last element is reached. _ _iter_ _() The iter method makes the object iterable, and returns an iterator.
  • 13.
    Iterator Pattern • Theiterator pattern has two parts: –An iterable class (to create the iterator object) –An iterator class (to traverse a container)
  • 14.
    def __init__(self, VALUE): self.VALUE= VALUE # END Init def __iter__(self): return ITERATOR(self.VALUE) # END Iter class ITERABLE: # END ITERABLE.
  • 15.
    def __init__(self, VALUE): self.VALUE= VALUE self.index = 0 # END Init def __iter__(self): return self # END Iter def __next__(self): if CONDITION: VALUE = SELF.VALUE self.index = self.index + 1 return VALUE else: raise StopIteration() # ENDIF; # END Next class ITERATOR: # END ITERATOR.
  • 16.
    Iterator Pattern • Let’slook at a simple example that counts numbers, there’ll be three parts: – The Iterable part – The Iteration part – The program execution part
  • 17.
    Iterator Pattern class MyCountIterable: def__init__(self, Value): self.Value = Value # END Init def __iter__(self): return MyCountIteration(self.Value) # END Iter # END MyCountIterable.
  • 18.
    Iterator Pattern class MyCountIteration: def__init__(self, Value): self.Index = 0 self.Value = Value # END Init def __iter__(self): # Iterators are iterables too. return self # END Iter def __next__(self): if self.Index < self.Value: # THEN Index = self.Index self.Index += 1 return Index else: raise StopIteration() # ENDIF; # END Next # END MyCountIteration.
  • 19.
    Iterator Pattern FirstCount =MyCountIterable(5) list(FirstCount) FirstCountIter = iter(FirstCount) while True: # DO try: print(next(FirstCountIter)) except StopIteration: break # ENDWHILE
  • 20.
    Iterator Pattern FirstCount =MyCountIterable(5) list(FirstCount) FirstCountIter = iter(FirstCount) while True: # DO try: print(next(FirstCountIter)) except StopIteration: break # ENDWHILE 0 1 2 3 4
  • 21.
    Iterator Pattern • Hangon… • Couldn’t we just do: • YES! for Counter in range(0,5): print(Counter) # ENDFOR;
  • 22.