Now, if you’ve been working with Python for a while now, you might be wondering what the ***** is this “generator function” thing. Well, let me tell ya, it’s like a regular old function… but better!
A generator function is just like any other function in that it takes some input and does something with it. But instead of returning a single value or object at the end, it returns an iterator which means you can keep getting values from it over and over again!
Here’s what I mean: let’s say we have this function that generates all the numbers between 1 and 10 (inclusive):
# This function generates numbers between 1 and 10 (inclusive) and returns an iterator that can be used to get values from it over and over again.
def generate_numbers():
# The range function creates a sequence of numbers from 1 to 10 (inclusive).
# The for loop iterates through each number in the sequence.
for i in range(1, 11):
# The yield keyword returns the current value of i and pauses the function until the next iteration.
yield i
Now, if you call this function like a regular old function:
# Define a function called "generate_numbers" that takes no arguments
def generate_numbers():
# Initialize a variable "num" with value 1
num = 1
# Create a while loop that runs until the condition is met
while True:
# Yield the value of "num" and pause the function
yield num
# Increment the value of "num" by 1
num += 1
# Call the function "generate_numbers" and assign it to a variable "numbers"
numbers = generate_numbers()
# Print the next value in the "numbers" generator and assign it to a variable "next_num"
next_num = next(numbers)
# Print the value of "next_num"
print(next_num) # prints "1"
# Print the next value in the "numbers" generator and assign it to a variable "next_num"
next_num = next(numbers)
# Print the value of "next_num"
print(next_num) # prints "2"
# Print the next value in the "numbers" generator and assign it to a variable "next_num"
next_num = next(numbers)
# Print the value of "next_num"
print(next_num) # prints "3"
# ... and so on, the generator will continue to yield the next number in the sequence each time it is called.
You’ll get an iterator object that you can keep calling `next()` on to get the next value. But what if we want to do something with all those values at once? Well, that’s where list comprehensions come in!
# This script creates a list of numbers using a list comprehension and prints the list.
# First, we define a function called generate_numbers that returns an iterator object.
def generate_numbers():
# This function generates numbers from 1 to 10 using a for loop.
for i in range(1, 11):
yield i
# Next, we use a list comprehension to create a list of numbers by calling the generate_numbers function.
numbers = [x for x in generate_numbers()]
# Finally, we print the list of numbers.
print(numbers) # prints "[1, 2, 3, ..., 10]"
And that’s it! Generator functions are super useful when you need to iterate over a large dataset or perform some kind of computation on each element. They can also be used in place of `for` loops for certain types of operations (like filtering or mapping).