Python Generators: A Simplified Guide
Python Generators
What is a Python Generator?
Python Generators are a special type of capability that yield an iterable object, simplifying the process of creating iterators. They enable traversing all items in a sequence, one at a time, without building a complete list in memory. This functionality is incredibly efficient for handling large datasets or infinite sequences.
Creating iterators in Python typically involves implementing the __iter__()
and __next__()
methods, which can be cumbersome and complex. Generators offer a more streamlined approach to achieving the same goal, reducing the need for verbose code. A generator raises the StopIteration exception automatically when it runs out of values to yield.
Download New Real Time Projects :-Click here
How to Create a Generator Function in Python?
Creating a generator function in Python is straightforward. It uses the def
keyword and includes a yield
statement instead of return
. A yield statement turns any function into a generator by default.
Here’s an example:
def simple():
for i in range(10):
if i % 2 == 0:
yield i
# Calling the generator using a for loop
for value in simple():
print(value)
Output:
0
2
4
6
8
PHP PROJECT:-Â CLICK HERE
Yield vs. Return
- Yield:
The yield statement saves the state of the function and halts its execution. When the function is invoked again, execution starts over from the previous state. There can be more than one yield statement for a generator function. - Return:
Thereturn
statement immediately exits the function and returns a value. There can only be one return statement for a typical function.
Here’s an illustration of how to use several yield statements:
def multiple_yield():
yield "First String"
yield "Second String"
yield "Third String"
obj = multiple_yield()
print(next(obj))
print(next(obj))
print(next(obj))
Output:
First String
Second String
Third String
INTERVIEW QUESTION:-CLICK HERE
Generator Function vs. Normal Function
Feature | Generator Function | Normal Function |
---|---|---|
Statements | Can have multiple yield statements | Contains one or more return statements |
Execution | Pauses and resumes execution | Immediately terminates upon return |
State Retention | Retains state between calls | Does not retain state |
Termination | Automatically raises StopIteration | Does not raise StopIteration |
Generator Expressions
A straightforward method for creating generators is to use generator expressions. They employ parentheses () in place of square brackets [], just like list comprehensions. Unlike list comprehensions, which generate all items at once, generator expressions calculate items one at a time.
Example:
nums = [1, 2, 3, 4, 5]
# List comprehension
list_comp = [x**3 for x in nums]
# Generator expression
gen_exp = (x**3 for x in nums)
print(gen_exp) # Displays generator object
print(list_comp)
Output:
<generator object <genexpr> at 0x01BA3CD8>
[1, 8, 27, 64, 125]
Additionally, you can extract values from a generator object using the next() function:
gen_exp = (x**3 for x in nums)
print(next(gen_exp))
print(next(gen_exp))
Output:
1
8
Complete Advance AI topics:- CLICK HERE
Example: Multiplication Table Using Generator
def table(n):
for i in range(1, 11):
yield n * i
for value in table(15):
print(value)
Output:
15
30
45
60
75
90
105
120
135
150
Advantages of Generators
Ease of Implementation:
Generators simplify the process of creating iterators. Unlike traditional iterators, they eliminate the need to implement __iter__()
and __next__()
methods.
Memory Efficiency:
Generators are memory-efficient. Instead of creating and storing all elements in memory (as in list comprehensions), generators compute each value on demand.
Example using sys.getsizeof()
:
import sys
# List comprehension
nums_list = [i * 2 for i in range(1000)]
print("Memory in Bytes:", sys.getsizeof(nums_list))
# Generator expression
nums_gen = (i * 2 for i in range(1000))
print("Memory in Bytes:", sys.getsizeof(nums_gen))
Output:
Memory in Bytes: 4508
Memory in Bytes: 56
Data Pipelining:
Generators facilitate efficient data pipelining, allowing processing of large datasets without exhausting system memory.
Example:
with open('sales.log') as file:
burger_col = (line.split()[3] for line in file)
per_hour = (int(x) for x in burger_col if x.isdigit())
print("Total burgers sold =", sum(per_hour))
Infinite Sequence Generation:
Generators can produce infinite sequences without overloading memory.
def infinite_sequence():
num = 0
while True:
yield num
num += 1
for value in infinite_sequence():
print(value)
Complete Python Course with Advance topics:- CLICK HERE
- python generators w3schools
- Python Generators: A Simplified Guide
- python generators list
- what is generator in python with example
- python generator expression
- Python Generators: A Simplified Guide
- Python Generators
- python output generator
- generator python typing
- python generator class
- decorators in python
- Python Generators
Post Comment