Wednesday, November 1, 2023

Unleashing the Power of Python Iterator & Generators

  • In this blog post, we'll dive into the world of Python generators, exploring what they are, how they work, and practical use cases that demonstrate their utility.
  • We will be learning the difference between iteration and generation in Python and how to construct our own Generators with the yield statement. Generators allow us to generate as we go along, instead of holding everything in memory.

What are Python Generators?

  • Let's explore a little deeper. We've learned how to create functions with def and the return statement. Generator functions allow us to write a function that can send back a value and then later resume to pick up where it left off. This type of function is a generator.

  • in Python, allowing us to generate a sequence of values over time. The main difference in syntax will be the use of a yield statement.

  • In most aspects, a generator function will appear very similar to a normal function. The main difference is when a generator function is compiled they become an object that supports an iteration protocol. 
  • That means when they are called in your code they don't actually return a value and then exit. Instead, generator functions will automatically suspend and resume their execution and state around the last point of value generation. 
  • The main advantage here is that instead of having to compute an entire series of values up front, the generator computes one value and then suspends its activity awaiting the next instruction. This feature is known as state suspension.

To start getting a better understanding of generators, let's go ahead and see how we can create some.

# Generator function for the cube of numbers (power of 3) def gencubes(n): for num in range(n): yield num**3

for x in gencubes(10): print(x)
Output: 0 1 8 27 64 125 216 343 512 729

  • Great! Now since we have a generator function we don't have to keep track of every single cube we created.
  • Generators are best for calculating large sets of results (particularly in calculations that involve loops themselves) in cases where we don’t want to allocate the memory for all of the results at the same time.

Let's create another example generator which calculates fibonacci numbers:

def genfibon(n): """ Generate a fibonnaci sequence up to n """ a = 1 b = 1 for i in range(n): yield a a,b = b,a+b

for num in genfibon(10): print(num)

Output : 1 1 2 3 5 8 13 21 34 55

  • What if this was a normal function, what would it look like?

def fibon(n): a = 1 b = 1 output = [] for i in range(n): output.append(a) a,b = b,a+b return output


  • Notice that if we call some huge value of n (like 100000) the second function will have to keep track of every single result, when in our case we actually only care about the previous result to generate the next one!

Iterating with Generators

  • Iterating through a generator is straightforward. You can use a for loop or call the next() function to retrieve the next value.
  • The next() function allows us to access the next element in a sequence. Lets check it out:?

def simple_gen(): for x in range(3): yield x # Assign simple_gen
g = simple_gen() print(next(g))
Output : 2


You may also like

Kubernetes Microservices
Python AI/ML
Spring Framework Spring Boot
Core Java Java Coding Question
Maven AWS