Custom Iterators

Intermediate Object-Oriented Programming in Python

Jake Roach

Data Engineer

Iterators

Classes that allow for a collection of objects or data stream to be traversed, and return one item at a time

  • Similar to list's, tuple's, but act differently
  • Navigate, transform, generate
  • Looped over using for loops
  • next() function

$$

Iterator protocol...

# Collection
chuck = NameIterator("Charles Carmicheal")
for letter in chuck:
    print(letter)
C
h
u
...
# Data stream
fun_game = DiceGame(rolls=3)
next(fun_game)  # ... and so on
4
Intermediate Object-Oriented Programming in Python

Iterator protocol

__iter__()

  • Returns an iterator, in this case, a reference to itself
  • ... return self

$$

__next__()

  • Returns the next value in the collection or data stream
  • Iteration, transformation, and generation takes place

$$

Both __iter__() and __next__() must be defined for a class to be considered an iterator!

Intermediate Object-Oriented Programming in Python

Example iterator

class CoinFlips:
    def __init__(self, number_of_flips):
        self.number_of_flips = number_of_flips  # Store the total number of flips
        self.counter = 0

    def __iter__(self):
        return self  # Return a reference of the iterator

    # Flip the next coin, return the output
    def __next__(self):
        if self.counter < self.number_of_flips:
            self.counter += 1
            return random.choice(["H", "T"])
Intermediate Object-Oriented Programming in Python

Using an example iterator

three_flips = CoinFlips(3)

# Flip the coin three times
next(three_flips)
next(three_flips)
next(three_flips)
H
H
T
Intermediate Object-Oriented Programming in Python

Looping through an iterator

three_flips = CoinFlips(3)

# Now, try to loop through every element of the iterator
for flip in three_flips:
    print(flip)
T
H
T
None
None
None
...
Intermediate Object-Oriented Programming in Python

StopIteration

...
    def __next__(self):
        # Only do this if the coin hasn't been flipped "number_of_flips" times
        if self.counter < self.number_of_flips:
            self.counter += 1
            return random.choice(["H", "T"])

        else:  # Otherwise, stop execution with StopIteration
            raise StopIteration
  • Signals end of collections/data stream
  • Prevents infinite loops
  • Easy to handle
Intermediate Object-Oriented Programming in Python

Looping through an iterator

three_flips = CoinFlips(3)

# Now, try to loop through every element of the iterator
for flip in three_flips:
    print(flip)
H
T
H
Intermediate Object-Oriented Programming in Python

Handling StopIteration exceptions

while True:
    try:
        next(three_flips)  # Pull the next element of three_flips

    # Catch a stop iteration exception
    except StopIteration:
        print("Completed all coin flips!")
        break
H
H
H
Completed all coin flips!
Intermediate Object-Oriented Programming in Python

Let's practice!

Intermediate Object-Oriented Programming in Python

Preparing Video For Download...