The Python Oracle

What is a "yield" statement in a function?

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Quiet Intelligence

--

Chapters
00:00 What Is A &Quot;Yield&Quot; Statement In A Function?
01:16 Answer 1 Score 4
02:07 Accepted Answer Score 12
02:44 Answer 3 Score 1
03:13 Answer 4 Score 4
04:01 Thank you

--

Full question
https://stackoverflow.com/questions/1206...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python

#avk47



ACCEPTED ANSWER

Score 12


Using yield makes the function a generator. The generator will continue to yield the a variable on each loop, waiting until the generator's next() method is called to continue on to the next loop iteration.

Or, until you return or StopIteration is raised.

Slightly modified to show use of StopIteration:

>>> def fib():
...     a = 0
...     b = 1
...     while True:
...         yield a
...         a = b
...         b += a
...         if a > 100:
...             raise StopIteration
...
>>>
>>> for value in fib():
...     print value
...
0
1
2
4
8
16
32
64
>>>

>>> # assign the resulting object to 'generator'
>>> generator = fib()
>>> generator.next()
0
>>> generator.next()
1
>>> for value in generator:
...     print value
...
2
4
8
16
32
64
>>>



ANSWER 2

Score 4


When the code calls fibonacci a special generator object is created. Please note, that no code gets executed - only a generator object is returned. When you are later calling its next method, the function executes until it encounters a yield statement. The object that is supplied to yield is returned. When you call next method again the function executes again until it encounters a yield. When there are no more yield statements and the end of function is reached, a StopIteration exception is raised.

Please note that the objects inside the function are preserved between the calls to next. It means, when the code continues execution on the next loop, all the objects that were in the scope from which yield was called have their values from the point where a previous next call returned.

The cool thing about generators is that they allow convenient iteration with for loops. The for loop obtains a generator from the result of fibonacci call and then executes the loop retrieving elements using next method of generatior object until StopIteration exception is encountered.




ANSWER 3

Score 4


Generators have a special property of being iterables which do not consume memories for their values.

They do this by calculating the new value, when it is required while being iterated.

i.e.

def f():
    a = 2
    yield a
    a += 1

for ele in f():
    print ele

would print

 2

So you are using a function as an iterable that keeps returning values. This is especially useful when you require heavy memory usage, and so you cannot afford the use of a list comprehension

i.e.

li = [ele*10 for ele in range(10)]

takes 10 memory spaces for ints as a list

but if you simple want to iterate over it, not access it individually

it would be very memory efficient to instead use

def f():
    i=0
    while i<10
        yield i*10
        i += 1

which would use 1 memory space as i keeps being reused

a short cut for this is

ge = (i*10 for i in range(10))

you can do any of the following

for ele in f():

for ele in li:

for ele in ge:

to obtain equivalent results




ANSWER 4

Score 1


This answer is a great explanation of the yield statement, and also of iterators and generators.

Specifically here, the first call to fibonaci() will initialize a to 0, b to 1, enter the while loop and return a. Any next call will start after the yield statement, affect b to a, a+b to b, and then go to the next iteration of the while statement, reach again the yield statement, and return a again.