python: the iterator pattern
TRANSCRIPT
![Page 1: Python: The Iterator Pattern](https://reader034.vdocuments.net/reader034/viewer/2022051501/5886d1861a28ab776a8b48cb/html5/thumbnails/1.jpg)
Iterator Pattern
Damian Gordon
![Page 2: Python: The Iterator Pattern](https://reader034.vdocuments.net/reader034/viewer/2022051501/5886d1861a28ab776a8b48cb/html5/thumbnails/2.jpg)
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.
![Page 3: Python: The Iterator Pattern](https://reader034.vdocuments.net/reader034/viewer/2022051501/5886d1861a28ab776a8b48cb/html5/thumbnails/3.jpg)
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.
![Page 4: Python: The Iterator Pattern](https://reader034.vdocuments.net/reader034/viewer/2022051501/5886d1861a28ab776a8b48cb/html5/thumbnails/4.jpg)
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.
![Page 5: Python: The Iterator Pattern](https://reader034.vdocuments.net/reader034/viewer/2022051501/5886d1861a28ab776a8b48cb/html5/thumbnails/5.jpg)
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.
![Page 6: Python: The Iterator Pattern](https://reader034.vdocuments.net/reader034/viewer/2022051501/5886d1861a28ab776a8b48cb/html5/thumbnails/6.jpg)
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.
![Page 7: Python: The Iterator Pattern](https://reader034.vdocuments.net/reader034/viewer/2022051501/5886d1861a28ab776a8b48cb/html5/thumbnails/7.jpg)
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.
![Page 8: Python: The Iterator Pattern](https://reader034.vdocuments.net/reader034/viewer/2022051501/5886d1861a28ab776a8b48cb/html5/thumbnails/8.jpg)
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.
![Page 9: Python: The Iterator Pattern](https://reader034.vdocuments.net/reader034/viewer/2022051501/5886d1861a28ab776a8b48cb/html5/thumbnails/9.jpg)
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.
![Page 10: Python: The Iterator Pattern](https://reader034.vdocuments.net/reader034/viewer/2022051501/5886d1861a28ab776a8b48cb/html5/thumbnails/10.jpg)
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.
![Page 11: Python: The Iterator Pattern](https://reader034.vdocuments.net/reader034/viewer/2022051501/5886d1861a28ab776a8b48cb/html5/thumbnails/11.jpg)
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;
![Page 12: Python: The Iterator Pattern](https://reader034.vdocuments.net/reader034/viewer/2022051501/5886d1861a28ab776a8b48cb/html5/thumbnails/12.jpg)
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.
![Page 13: Python: The Iterator Pattern](https://reader034.vdocuments.net/reader034/viewer/2022051501/5886d1861a28ab776a8b48cb/html5/thumbnails/13.jpg)
Iterator Pattern
• The iterator pattern has two parts:
–An iterable class (to create the iterator object)
–An iterator class (to traverse a container)
![Page 14: Python: The Iterator Pattern](https://reader034.vdocuments.net/reader034/viewer/2022051501/5886d1861a28ab776a8b48cb/html5/thumbnails/14.jpg)
def __init__(self, VALUE): self.VALUE = VALUE# END Init
Iterable Pattern
def __iter__(self): return ITERATOR(self.VALUE) # END Iter
class ITERABLE:
# END ITERABLE.
![Page 15: Python: The Iterator Pattern](https://reader034.vdocuments.net/reader034/viewer/2022051501/5886d1861a28ab776a8b48cb/html5/thumbnails/15.jpg)
def __init__(self, VALUE): self.VALUE = VALUE self.index = 0 # END Init
Iterator Pattern
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.
![Page 16: Python: The Iterator Pattern](https://reader034.vdocuments.net/reader034/viewer/2022051501/5886d1861a28ab776a8b48cb/html5/thumbnails/16.jpg)
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
![Page 17: Python: The Iterator Pattern](https://reader034.vdocuments.net/reader034/viewer/2022051501/5886d1861a28ab776a8b48cb/html5/thumbnails/17.jpg)
Iterator Patternclass MyCountIterable: def __init__(self, Value): self.Value = Value # END Init
def __iter__(self): return MyCountIteration(self.Value) # END Iter
# END MyCountIterable.
![Page 18: Python: The Iterator Pattern](https://reader034.vdocuments.net/reader034/viewer/2022051501/5886d1861a28ab776a8b48cb/html5/thumbnails/18.jpg)
Iterator Patternclass 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.
![Page 19: Python: The Iterator Pattern](https://reader034.vdocuments.net/reader034/viewer/2022051501/5886d1861a28ab776a8b48cb/html5/thumbnails/19.jpg)
Iterator PatternFirstCount = MyCountIterable(5)list(FirstCount)FirstCountIter = iter(FirstCount)
while True:# DO try: print(next(FirstCountIter)) except StopIteration:
break# ENDWHILE
![Page 20: Python: The Iterator Pattern](https://reader034.vdocuments.net/reader034/viewer/2022051501/5886d1861a28ab776a8b48cb/html5/thumbnails/20.jpg)
Iterator PatternFirstCount = MyCountIterable(5)list(FirstCount)FirstCountIter = iter(FirstCount)
while True:# DO try: print(next(FirstCountIter)) except StopIteration:
break# ENDWHILE
01234
![Page 21: Python: The Iterator Pattern](https://reader034.vdocuments.net/reader034/viewer/2022051501/5886d1861a28ab776a8b48cb/html5/thumbnails/21.jpg)
Iterator Pattern
• Hang on…• Couldn’t we just do:
• YES!
for Counter in range(0,5): print(Counter)# ENDFOR;
![Page 22: Python: The Iterator Pattern](https://reader034.vdocuments.net/reader034/viewer/2022051501/5886d1861a28ab776a8b48cb/html5/thumbnails/22.jpg)
etc.