What does the “yield” keyword do in python?
First of all you must to understand what generators [ Generators – Python Wiki ] are. And before you understand what generators are, you must understands what iterables [Iterables – PLYMI] are.
Iterables
Definition: An iterable is any Python object capable of returning its members one at a time, permitting it to be iterated over in a for-loop.
At the point when you create a list, you can read its items one by one. Reading its items one by one is called iteration:
# Examples of built-in functions
# that act on iterables
>> mypythonlist = [1, 2, 3]
>>> for i in mypythonlist:
print(i)
1
2
3
mypythonlist
is an iterable. When you use a list comprehension, you create a list, and so an iterable:
>>> mypythonlist = [x*x for x in range(3)]
>>> for i in mypythonlist:
print(i)
0
1
4
Everything you can use “for... in...
” on is an iterable; lists
, strings
, files…
These iterables are handy because you can read them as much as you wish, but you store all the values in memory and this is not always what you want when you have a lot of values.
Generators
Generators are iterators, a kind of iterable you can only iterate over once. Generators do not store all the values in memory, they generate the values on the fly:
>>> mypythongenerator = (x*x for x in range(3))
>>> for i in mypythongenerator:
print(i)
0
1
4
It is just the same except you used ()
instead of []
. BUT, you cannot perform for i in mypythongenerator
a second time since generators can only be used once: they calculate 0, then forget about it and calculate 1, and end calculating 4, one by one.
Yield in Python
yield
is a keyword that is used like return
, except the function will return a generator.
>>> def create_generator():
mypythonlist = range(3)
for i in mypythonlist:
yield i*i
>>> mypythongenerator = create_generator() # create a generator
>>> print(mypythongenerator) # mygenerator is an object!
<generator object create_generator at 0xb763hf334>
>>> for i in mypythongenerator:
print(i)
0
1
4
Here it’s a useless example, but it’s handy when you know your function will return a huge set of values that you will only need to read once.
To master yield
, you must understand that when you call the function, the code you have written in the function body does not run. The function only returns the generator object, this is a bit tricky.
Then, your code will continue from where it left off each time for
uses the generator.
Now the hard part:
The first time the for
calls the generator object created from your function, it will run the code in your function from the beginning until it hits yield
, then it’ll return the first value of the loop. Then, each subsequent call will run another iteration of the loop you have written in the function and return the next value. This will continue until the generator is considered empty, which happens when the function runs without hitting yield
. That can be because the loop has come to an end, or because you no longer satisfy an "if/else"
.
Now the hard part:
The first time the for
calls the generator object created from your function, it will run the code in your function from the beginning until it hits yield
, then it’ll return the first value of the loop. Then, each subsequent call will run another iteration of the loop you have written in the function and return the next value. This will continue until the generator is considered empty, which happens when the function runs without hitting yield
. That can be because the loop has come to an end, or because you no longer satisfy an "if/else"
.
Sources:
https://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do
https://www.pythonlikeyoumeanit.com/Module2_EssentialsOfPython/Iterables.html
Asked:
what does yield mean in python?