Introduction to Generators
Lesson Introduction
Welcome! Today, we will learn about an exciting Python feature called generators. Generators help us write code that uses memory efficiently, which is crucial when handling large amounts of data. By the end of this lesson, you'll understand what generators are and how to create and use them in Python. You’ll learn how they can make your code more efficient, especially when working with large datasets.
Why Understand Generators?
Imagine you have a vast book. You will read one sentence at a time rather than holding the whole book in your head at once. Generators in Python work similarly: they let us handle large data collections one item at a time.
Concept of Generators
Generators are special Python functions that return one item at a time, which helps process large datasets efficiently. Unlike normal functions that return a list of items and hold all items in memory at once, generators yield items one by one, using less memory and processing time.
Here's a quick comparison:
- A normal function returns all items at once (like a big bag of candies).
- A generator yields one item at a time (giving one candy at a time).
Example: Using a Normal Function to Return a List
Let's look at a normal function that returns a list of numbers from 1 to 5.
This function returns a list of five numbers. Now, let's see how generators do it differently.
Example of a Simple Generator
Here is a simple_generator function:
The simple_generator function uses the yield keyword. Unlike return, which exits the function, yield pauses the function and saves its state.
The yield keyword is what makes a function a generator. Here’s what happens when a function containing yield is called:
- Creates a Generator Object: Instead of running the function, it returns a generator object.
- Pausing and Resuming: When the generator's
__next__()method is called (e.g., vianext()function or aforloop), the function runs until it hitsyield. - Saves State and Returns Value: The function pauses at
yield, saves its current state (local variables, execution point), and returns the yielded value. - Resumes from Last State: When
__next__()is called again, it resumes right after the lastyieldstatement, with all its variables intact.
