yeild
do?The yield
statement is a fundamental feature in Python that is used within generator functions. It allows a function to act as an iterator and produce a series of values that can be iterated over in a loop, one value at a time.
When you use the yield
statement in a function, it transforms that function into a generator. A generator is a special type of iterator that generates values lazily as you iterate over it, rather than computing all values at once and storing them in memory. This lazy evaluation makes generators memory-efficient and allows them to handle large or infinite sequences of data.
Here's how the yield
statement works:
()
brackets when called, like my_generator()
.for
loop), the function's body starts executing. The function runs until it reaches the yield
statement.yield
statement is encountered, the value following the yield
keyword is returned as the next item in the iteration. The state of the function is frozen at that point, and the function effectively pauses its execution.yield
statement.StopIteration
exception, which signals the end of the iteration.Here's a simple example of a generator function using yield
to generate squares of numbers:
def squares_generator(n):
for i in range(n):
yield i ** 2
# Using the generator in a loop
for square in squares_generator(5):
print(square)
Output:
0
1
4
9
16
In this example, squares_generator
is a generator function that yields the square of each number from 0 to n-1
. The loop iterates over the generator, and each time it encounters a yield
statement, it retrieves the next square value from the generator.
Generators are powerful constructs in Python, especially when dealing with large datasets, streams, or infinite sequences, as they allow you to efficiently produce and consume data one piece at a time, rather than storing everything in memory.
yeild
is PythonicThe yield
statement and creating generator functions is considered Pythonic and follows the principles of Python's design. The concept of generators is an essential part of Python, and it promotes several Pythonic principles and practices: