Skip to content

Latest commit

 

History

History
121 lines (81 loc) · 6.71 KB

README.md

File metadata and controls

121 lines (81 loc) · 6.71 KB

Iterator Design Pattern

Videos

Section Video Links
Iterator Overview Iterator Overview Iterator Overview Iterator Overview
Iterator Use Case Iterator Use Case Iterator Use Case Iterator Use Case
Python iter() Function Python iter() Function Python iter() Function Python iter() Function

Book

Cover Links
Design Patterns In Python (ASIN : B08XLJ8Z2J)    https://www.amazon.com/dp/B08XLJ8Z2J
   https://www.amazon.co.uk/dp/B08XLJ8Z2J
   https://www.amazon.in/dp/B08Z282SBC
   https://www.amazon.de/dp/B08XLJ8Z2J
   https://www.amazon.fr/dp/B08XLJ8Z2J
   https://www.amazon.es/dp/B08XLJ8Z2J
   https://www.amazon.it/dp/B08XLJ8Z2J
   https://www.amazon.co.jp/dp/B08XLJ8Z2J
   https://www.amazon.ca/dp/B08XLJ8Z2J
   https://www.amazon.com.au/dp/B08Z282SBC

Overview

... Refer to Book or Design Patterns In Python website to read textual content.

Terminology

... Refer to Book or Design Patterns In Python website to read textual content.

Iterator UML Diagram

Iterator Pattern Overview

Source Code

... Refer to Book or Design Patterns In Python website to read textual content.

Output

python ./iterator/iterator_concept.py
This method has been invoked
This method has been invoked
This method has been invoked
This method has been invoked

Example Use Case

... Refer to Book or Design Patterns In Python website to read textual content.

Example UML Diagram

Iterator Pattern Overview

Output

python ./iterator/client.py
2, 4, 6, 8, 10, 1, 3, 5, 7, 9, 0, 2, 4, 6, 8, 10, 1, 3, 5, 7, 9, 0,

New Coding Concepts

Python iter()

Python Lists, Dictionaries, Sets and Tuples are already iterable, so if you want basic iteration for use in a for loop, then you only need to add your objects into one of those and it can be used right away.

NAMES = ['SEAN','COSMO','EMMY']
for name in NAMES:
    print(name, end=", ")
#SEAN, COSMO, EMMY,

also, you can instantiate an iterable from the List, Dictionary, Tuple or Set by using the Python iter() method, or its own __iter__() dunder method, and then iterate over that using the __next__() method.

NAMES = ['SEAN','COSMO','EMMY']
ITERATOR = iter(NAMES)
print(ITERATOR.__next__())
print(ITERATOR.__next__())
print(ITERATOR.__next__())

or

NAMES = ['SEAN','COSMO','EMMY']
ITERATOR = NAMES.__iter__()
print(ITERATOR.__next__())
print(ITERATOR.__next__())
print(ITERATOR.__next__())

The Python iter() method also can accept a sentinel parameter.

The sentinel parameter is useful for dynamically created objects that are returned from an iterator and indicates where the last item is in the iterator by raising a StopIteration exception.

Usage : iter(object, sentinel)

When using the sentinel, the object passed as the first argument in the iter() method will need to be callable.

class Doubler():
    count = 1

    @classmethod
    def next(cls):
        cls.count *= 2
        return cls.count

    __call__ = next

ITERATOR = iter(Doubler(), 32)
print(list(ITERATOR))
# Outputs [2, 4, 8, 16]

The __call__ = next line in the example above is setting the default method of the class to be next and that makes the class callable. See Dunder call Method for more information.

Also note that the list being printed at the end is automatically filled from the iterator. The list constructor utilizes the default callable method and the StopIteration exception automatically during its creation without needing to write this in the code.

Summary

... Refer to Book or Design Patterns In Python website to read textual content.