The yield Keyword
Write generator functions with yield for custom iterators.
Introduction
Generator functions use yield to produce a sequence of values lazily, pausing execution between each yield.
What is yield?
A function with yield is a generator function. Each call to next() resumes from the last yield.
def count_up(n):
for i in range(n):
yield i
for x in count_up(3):
print(x)All lessons in this course
- List Comprehensions
- Set and Dict Comprehensions
- Generator Expressions
- The yield Keyword