13111

What functionality does theyield keyword in Python provide?

For example, I'm trying to understand this code1:

def _get_child_candidates(self, distance, min_dist, max_dist):    if self._leftchild and distance - max_dist < self._median:        yield self._leftchild    if self._rightchild and distance + max_dist >= self._median:        yield self._rightchild

And this is the caller:

result, candidates = [], [self]while candidates:    node = candidates.pop()    distance = node._get_dist(obj)    if distance <= max_dist and distance >= min_dist:        result.extend(node._values)    candidates.extend(node._get_child_candidates(distance, min_dist, max_dist))return result

What happens when the method_get_child_candidates is called?Is a list returned? A single element? Is it called again? When will subsequent calls stop?


1. This piece of code was written by Jochen Schulz (jrschulz), who made a great Python library for metric spaces. This is the link to the complete source:Module mspace.
Mateen Ulhaq's user avatar
Mateen Ulhaq
27.9k21 gold badges122 silver badges155 bronze badges
askedOct 23, 2008 at 22:21
Alex. S.'s user avatar
2
  • 5
    Guys the line:if distance <= max_dist and distance >= min_dist: can be shortened toif min_dist <= distance <= max_dist:CommentedApr 22, 2024 at 10:49
  • 8
    Yield in Python used to create a generator function. Generator function behaves like an iterator, which can be used in loop to retrieve items one at a time. When a generator function is called, it returns a generator object without executing the function immediately. When next() is called on the generator object, the function executes until it reaches a yield statement, which returns the yielded value and pauses the function's execution, maintaining its state. When next() is called again, the function resumes execution right after the yield statement, until it reaches another yield or returns.CommentedMay 23, 2024 at 13:23

50 Answers50

18270
+600

To understand whatyield does, you must understand whatgenerators are. And before you can understand generators, you must understanditerables.

Iterables

When you create a list, you can read its items one by one. Reading its items one by one is called iteration:

>>> mylist = [1, 2, 3]>>> for i in mylist:...    print(i)123

mylist is aniterable. When you use a list comprehension, you create a list, and so an iterable:

>>> mylist = [x*x for x in range(3)]>>> for i in mylist:...    print(i)014

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 areiterators, a kind of iterableyou can only iterate over once. Generators do not store all the values in memory,they generate the values on the fly:

>>> mygenerator = (x*x for x in range(3))>>> for i in mygenerator:...    print(i)014

It is just the same except you used() instead of[]. BUT, youcannot performfor i in mygenerator a second time since generators can only be used once: they calculate 0, then forget about it and calculate 1, and end after calculating 4, one by one.

Yield

yield is a keyword that is used likereturn, except the function will return a generator.

>>> def create_generator():...    mylist = range(3)...    for i in mylist:...        yield i*i...>>> mygenerator = create_generator() # create a generator>>> print(mygenerator) # mygenerator is an object!<generator object create_generator at 0xb7555c34>>>> for i in mygenerator:...     print(i)014

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 masteryield, you must understand thatwhen 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 timefor uses the generator.

Now the hard part:

The first time thefor calls the generator object created from your function, it will run the code in your function from the beginning until it hitsyield, 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 hittingyield. That can be because the loop has come to an end, or because you no longer satisfy an"if/else".


Your code explained

Generator:

# Here you create the method of the node object that will return the generatordef _get_child_candidates(self, distance, min_dist, max_dist):    # Here is the code that will be called each time you use the generator object:    # If there is still a child of the node object on its left    # AND if the distance is ok, return the next child    if self._leftchild and distance - max_dist < self._median:        yield self._leftchild    # If there is still a child of the node object on its right    # AND if the distance is ok, return the next child    if self._rightchild and distance + max_dist >= self._median:        yield self._rightchild    # If the function arrives here, the generator will be considered empty    # There are no more than two values: the left and the right children

Caller:

# Create an empty list and a list with the current object referenceresult, candidates = list(), [self]# Loop on candidates (they contain only one element at the beginning)while candidates:    # Get the last candidate and remove it from the list    node = candidates.pop()    # Get the distance between obj and the candidate    distance = node._get_dist(obj)    # If the distance is ok, then you can fill in the result    if distance <= max_dist and distance >= min_dist:        result.extend(node._values)    # Add the children of the candidate to the candidate's list    # so the loop will keep running until it has looked    # at all the children of the children of the children, etc. of the candidate    candidates.extend(node._get_child_candidates(distance, min_dist, max_dist))return result

This code contains several smart parts:

  • The loop iterates on a list, but the list expands while the loop is being iterated. It's a concise way to go through all these nested data even if it's a bit dangerous since you can end up with an infinite loop. In this case,candidates.extend(node._get_child_candidates(distance, min_dist, max_dist)) exhausts all the values of the generator, butwhile keeps creating new generator objects which will produce different values from the previous ones since it's not applied on the same node.

  • Theextend() method is a list object method that expects an iterable and adds its values to the list.

Usually, we pass a list to it:

>>> a = [1, 2]>>> b = [3, 4]>>> a.extend(b)>>> print(a)[1, 2, 3, 4]

But in your code, it gets a generator, which is good because:

  1. You don't need to read the values twice.
  2. You may have a lot of children and you don't want them all stored in memory.

And it works because Python does not care if the argument of a method is a list or not. Python expects iterables so it will work with strings, lists, tuples, and generators! This is called duck typing and is one of the reasons why Python is so cool. But this is another story, for another question...

You can stop here, or read a little bit to see an advanced use of a generator:

Controlling a generator exhaustion

>>> class Bank(): # Let's create a bank, building ATMs...    crisis = False...    def create_atm(self):...        while not self.crisis:...            yield "$100">>> hsbc = Bank() # When everything's ok the ATM gives you as much as you want>>> corner_street_atm = hsbc.create_atm()>>> print(corner_street_atm.next())$100>>> print(corner_street_atm.next())$100>>> print([corner_street_atm.next() for cash in range(5)])['$100', '$100', '$100', '$100', '$100']>>> hsbc.crisis = True # Crisis is coming, no more money!>>> print(corner_street_atm.next())<type 'exceptions.StopIteration'>>>> wall_street_atm = hsbc.create_atm() # It's even true for new ATMs>>> print(wall_street_atm.next())<type 'exceptions.StopIteration'>>>> hsbc.crisis = False # The trouble is, even post-crisis the ATM remains empty>>> print(corner_street_atm.next())<type 'exceptions.StopIteration'>>>> brand_new_atm = hsbc.create_atm() # Build a new one to get back in business>>> for cash in brand_new_atm:...    print cash$100$100$100$100$100$100$100$100$100...

Note: For Python 3, useprint(corner_street_atm.__next__()) orprint(next(corner_street_atm))

It can be useful for various things like controlling access to a resource.

Itertools, your best friend

Theitertools module contains special functions to manipulate iterables. Ever wish to duplicate a generator?Chain two generators? Group values in a nested list with a one-liner?Map / Zip without creating another list?

Then justimport itertools.

An example? Let's see the possible orders of arrival for a four-horse race:

>>> horses = [1, 2, 3, 4]>>> races = itertools.permutations(horses)>>> print(races)<itertools.permutations object at 0xb754f1dc>>>> print(list(itertools.permutations(horses)))[(1, 2, 3, 4), (1, 2, 4, 3), (1, 3, 2, 4), (1, 3, 4, 2), (1, 4, 2, 3), (1, 4, 3, 2), (2, 1, 3, 4), (2, 1, 4, 3), (2, 3, 1, 4), (2, 3, 4, 1), (2, 4, 1, 3), (2, 4, 3, 1), (3, 1, 2, 4), (3, 1, 4, 2), (3, 2, 1, 4), (3, 2, 4, 1), (3, 4, 1, 2), (3, 4, 2, 1), (4, 1, 2, 3), (4, 1, 3, 2), (4, 2, 1, 3), (4, 2, 3, 1), (4, 3, 1, 2), (4, 3, 2, 1)]

Understanding the inner mechanisms of iteration

Iteration is a process implying iterables (implementing the__iter__() method) and iterators (implementing the__next__() method).Iterables are any objects you can get an iterator from. Iterators are objects that let you iterate on iterables.

There is more about it in this article abouthowfor loops work.

Mateen Ulhaq's user avatar
Mateen Ulhaq
27.9k21 gold badges122 silver badges155 bronze badges
answeredOct 23, 2008 at 22:48
Bite code's user avatar
Sign up to request clarification or add additional context in comments.

13 Comments

yield is not as magical this answer suggests. When you call a function that contains ayield statement anywhere, you get a generator object, but no code runs. Then each time you extract an object from the generator, Python executes code in the function until it comes to ayield statement, then pauses and delivers the object. When you extract another object, Python resumes just after theyield and continues until it reaches anotheryield (often the same one, but one iteration later). This continues until the function runs off the end, at which point the generator is deemed exhausted.
"These iterables are handy... but you store all the values in memory and this is not always what you want", is either wrong or confusing. An iterable returns an iterator upon calling the iter() on the iterable, and an iterator doesn't always have to store its values in memory, depending on the implementation of theiter method, it can also generate values in the sequence on demand.
It would be nice to add to thisgreat answer whyIt is just the same except you used() instead of[], specifically what() is (there may be confusion with a tuple).
@MatthiasFripp "This continues until the function runs off the end" -- or it encounters areturn statement. (return is permitted in a function containingyield, provided that it does not specify a return value.)
The yield statement suspends function’s execution and sends a value back to the caller, but retains enough state to enable function to resume where it is left off. When resumed, the function continues execution immediately after the last yield run. This allows its code to produce a series of values over time, rather than computing them at once and sending them back like a list.
|
2591

Shortcut to understandingyield

When you see a function withyield statements, apply this easy trick to understand what will happen:

  1. Insert a lineresult = [] at the start of the function.
  2. Replace eachyield expr withresult.append(expr).
  3. Insert a linereturn result at the bottom of the function.
  4. Yay - no moreyield statements! Read and figure out the code.
  5. Compare the function to the original definition.

This trick may give you an idea of the logic behind the function, but what actually happens withyield is significantly different than what happens in the list-based approach. In many cases, the yield approach will be a lot more memory efficient and faster too. In other cases, this trick will get you stuck in an infinite loop, even though the original function works just fine. Read on to learn more...

Don't confuse your iterables, iterators, and generators

First, theiterator protocol - when you write

for x in mylist:    ...loop body...

Python performs the following two steps:

  1. Gets an iterator formylist:

    Calliter(mylist) -> this returns an object with anext() method (or__next__() in Python 3).

    [This is the step most people forget to tell you about]

  2. Uses the iterator to loop over items:

    Keep calling thenext() method on the iterator returned from step 1. The return value fromnext() is assigned tox and the loop body is executed. If an exceptionStopIteration is raised from withinnext(), it means there are no more values in the iterator and the loop is exited.

The truth is Python performs the above two steps anytime it wants toloop over the contents of an object - so it could be a for loop, but it could also be code likeotherlist.extend(mylist) (whereotherlist is a Python list).

Heremylist is aniterable because it implements the iterator protocol. In a user-defined class, you can implement the__iter__() method to make instances of your class iterable. This method should return aniterator. An iterator is an object with anext() method. It is possible to implement both__iter__() andnext() on the same class, and have__iter__() returnself. This will work for simple cases, but not when you want two iterators looping over the same object at the same time.

So that's the iterator protocol, many objects implement this protocol:

  1. Built-in lists, dictionaries, tuples, sets, and files.
  2. User-defined classes that implement__iter__().
  3. Generators.

Note that afor loop doesn't know what kind of object it's dealing with - it just follows the iterator protocol, and is happy to get item after item as it callsnext(). Built-in lists return their items one by one, dictionaries return thekeys one by one, files return thelines one by one, etc. And generators return... well that's whereyield comes in:

def f123():    yield 1    yield 2    yield 3for item in f123():    print item

Instead ofyield statements, if you had threereturn statements inf123() only the first would get executed, and the function would exit. Butf123() is no ordinary function. Whenf123() is called, itdoes not return any of the values in the yield statements! It returns a generator object. Also, the function does not really exit - it goes into a suspended state. When thefor loop tries to loop over the generator object, the function resumes from its suspended state at the very next line after theyield it previously returned from, executes the next line of code, in this case, ayield statement, and returns that as the next item. This happens until the function exits, at which point the generator raisesStopIteration, and the loop exits.

So the generator object is sort of like an adapter - at one end it exhibits the iterator protocol, by exposing__iter__() andnext() methods to keep thefor loop happy. At the other end, however, it runs the function just enough to get the next value out of it and puts it back in suspended mode.

Why use generators?

Usually, you can write code that doesn't use generators but implements the same logic. One option is to use the temporary list 'trick' I mentioned before. That will not work in all cases, for e.g. if you have infinite loops, or it may make inefficient use of memory when you have a really long list. The other approach is to implement a new iterable classSomethingIter that keeps the state in instance members and performs the next logical step in itsnext() (or__next__() in Python 3) method. Depending on the logic, the code inside thenext() method may end up looking very complex and prone to bugs. Here generators provide a clean and easy solution.

Michael M.'s user avatar
Michael M.
11.2k11 gold badges22 silver badges46 bronze badges
answeredOct 25, 2008 at 21:22
user28409's user avatar

3 Comments

"When you see a function with yield statements, apply this easy trick to understand what will happen" Doesn't this completely ignore the fact that you cansend into a generator, which is a huge part of the point of generators?
"it could be a for loop, but it could also be code likeotherlist.extend(mylist)" -> This is incorrect.extend() modifies the list in-place and does not return an iterable. Trying to loop overotherlist.extend(mylist) will fail with aTypeError becauseextend() implicitly returnsNone, and you can't loop overNone.
@pedro You have misunderstood that sentence. It means that python performs the two mentioned steps onmylist (not onotherlist) when executingotherlist.extend(mylist).
800

Think of it this way:

An iterator is just a fancy sounding term for an object that has anext() method. So a yield-ed function ends up being something like this:

Original version:

def some_function():    for i in xrange(4):        yield ifor i in some_function():    print i

This is basically what the Python interpreter does with the above code:

class it:    def __init__(self):        # Start at -1 so that we get 0 when we add 1 below.        self.count = -1    # The __iter__ method will be called once by the 'for' loop.    # The rest of the magic happens on the object returned by this method.    # In this case it is the object itself.    def __iter__(self):        return self    # The next method will be called repeatedly by the 'for' loop    # until it raises StopIteration.    def next(self):        self.count += 1        if self.count < 4:            return self.count        else:            # A StopIteration exception is raised            # to signal that the iterator is done.            # This is caught implicitly by the 'for' loop.            raise StopIterationdef some_func():    return it()for i in some_func():    print i

For more insight as to what's happening behind the scenes, thefor loop can be rewritten to this:

iterator = some_func()try:    while 1:        print iterator.next()except StopIteration:    pass

Does that make more sense or just confuse you more? :)

I should note that thisis an oversimplification for illustrative purposes. :)

Georgy's user avatar
Georgy
14k7 gold badges69 silver badges80 bronze badges
answeredOct 23, 2008 at 22:28
Jason Baker's user avatar

4 Comments

__getitem__ could be defined instead of__iter__. For example:class it: pass; it.__getitem__ = lambda self, i: i*10 if i < 10 else [][0]; for i in it(): print(i), It will print: 0, 10, 20, ..., 90
I tried this example in Python 3.6 and if I createiterator = some_function(), the variableiterator does not have a function callednext() anymore, but only a__next__() function. Thought I'd mention it.
Where does thefor loop implementation you wrote call the__iter__ method ofiterator, the instantiated instance ofit?
Unfortunately this answer is not true at all. This is not what python interpreter does with generators. It is not creating a class starting from the generator function and implement__iter__ and__next__. What it is acutally doing under the hood is explained in this poststackoverflow.com/questions/45723893/…. To cite @Raymond Hettinger"generators are not implemented internally as shown in your pure python class. Instead, they share most of the same logic as regular functions"
682

Theyield keyword is reduced to two simple facts:

  1. If the compiler detects theyield keywordanywhere inside a function, that function no longer returns via thereturn statement.Instead, itimmediately returns alazy "pending list" object called a generator
  2. A generator is iterable. What is aniterable? It's anything like alist,set,range, dictionary view, or any other object with abuilt-in protocol for visiting each element in a certain order.

In a nutshell: Most commonly,a generator is a lazy, incrementally-pending list, andyield statements allow you to use function notation to program the list values the generator should incrementally spit out.Furthermore, advanced usage lets you use generators as coroutines (see below).

generator = myYieldingFunction(...)  # basically a list (but lazy)x = list(generator)  # evaluate every element into a list   generator       v[x[0], ..., ???]         generator             v[x[0], x[1], ..., ???]               generator                   v[x[0], x[1], x[2], ..., ???]                       StopIteration exception[x[0], x[1], x[2]]     done

Basically, whenever theyield statement is encountered, the function pauses and saves its state, then emits "the next return value in the 'list'" according to the python iterator protocol (to some syntactic construct like a for-loop that repeatedly callsnext() and catches aStopIteration exception, etc.). You might have encountered generators withgenerator expressions; generator functions are more powerful because you can pass arguments back into the paused generator function, using them to implement coroutines. More on that later.


Basic Example ('list')

Let's define a functionmakeRange that's just like Python'srange. CallingmakeRange(n) RETURNS A GENERATOR:

def makeRange(n):    # return 0,1,2,...,n-1    i = 0    while i < n:        yield i        i += 1>>> makeRange(5)<generator object makeRange at 0x19e4aa0>

To force the generator to immediately return its pending values, you can pass it intolist() (just like you could any iterable):

>>> list(makeRange(5))[0, 1, 2, 3, 4]

Comparing the example to "just returning a list"

The above example can be thought of as merely creating a list that you append to and return:

# return a list                  #  # return a generatordef makeRange(n):                #  def makeRange(n):    """return [0,1,2,...,n-1]""" #      """return 0,1,2,...,n-1"""    TO_RETURN = []               #     i = 0                        #      i = 0    while i < n:                 #      while i < n:        TO_RETURN += [i]         #          yield i        i += 1                   #          i += 1    return TO_RETURN             # >>> makeRange(5)[0, 1, 2, 3, 4]

There is one major difference, though; see the last section.


How you might use generators

An iterable is the last part of a list comprehension, and all generators are iterable, so they're often used like so:

#                  < ITERABLE >>>> [x+10 for x in makeRange(5)][10, 11, 12, 13, 14]

To get a better feel for generators, you can play around with theitertools module (be sure to usechain.from_iterable rather thanchain when warranted). For example, you might even use generators to implement infinitely-long lazy lists likeitertools.count(). You could implement your owndef enumerate(iterable): zip(count(), iterable), or alternatively do so with theyield keyword in a while-loop.

Please note: generators can actually be used for many more things, such asimplementing coroutines, non-deterministic programming, and other elegant things. However, the "lazy lists" viewpoint I present here is the most common use you will find.


Behind the scenes

This is how the "Python iteration protocol" works. That is, what is going on when you dolist(makeRange(5)). This is what I describe earlier as a "lazy, incremental list".

>>> x=iter(range(5))>>> next(x)  # calls x.__next__(); x.next() is deprecated0>>> next(x)1>>> next(x)2>>> next(x)3>>> next(x)4>>> next(x)Traceback (most recent call last):  File "<stdin>", line 1, in <module>StopIteration

The built-in functionnext() just calls the objects.__next__() function, which is a part of the "iteration protocol" and is found on all iterators. You can manually use thenext() function (and other parts of the iteration protocol) to implement fancy things, usually at the expense of readability, so try to avoid doing that...


Coroutines

Coroutine example:

def interactiveProcedure():    userResponse = yield makeQuestionWebpage()    print('user response:', userResponse)    yield 'success'coroutine = interactiveProcedure()webFormData = next(coroutine)  # same as .send(None)userResponse = serveWebForm(webFormData)# ...at some point later on web form submit...successStatus = coroutine.send(userResponse)

A coroutine (generators that generally accept input via theyield keyword e.g.nextInput = yield nextOutput, as a form of two-way communication) is basically a computation that is allowed to pause itself and request input (e.g. to what it should do next). When the coroutine pauses itself (when the running coroutine eventually hits ayield keyword), the computation is paused and control is inverted (yielded) back to the 'calling' function (the frame which requested thenext value of the computation). The paused generator/coroutine remains paused until another invoking function (possibly a different function/context) requests the next value to unpause it (usually passing input data to direct the paused logic interior to the coroutine's code).

You can think of Python coroutines as lazy incrementally-pending lists, where the next element doesn't just depend on the previous computation but also on input that you may opt to inject during the generation process.


Minutiae

Normally, most people would not care about the following distinctions and probably want to stop reading here.

In Python-speak, aniterable is any object which "understands the concept of a for-loop" like a list[1,2,3], and aniterator is a specific instance of the requested for-loop like[1,2,3].__iter__(). Agenerator is exactly the same as any iterator, except for the way it was written (with function syntax).

When you request an iterator from a list, it creates a new iterator. However, when you request an iterator from an iterator (which you would rarely do), it just gives you a copy of itself.

Thus, in the unlikely event that you are failing to do something like this...

> x = myRange(5)> list(x)[0, 1, 2, 3, 4]> list(x)[]

... then remember that a generator is aniterator; that is, it is one-time-use. If you want to reuse it, you should callmyRange(...) again. If you need to use the result twice, convert the result to a list and store it in a variablex = list(myRange(5)). Those who absolutely need to clone a generator (for example, who are doing terrifyingly hackish metaprogramming) can useitertools.tee (still works in Python 3) if absolutely necessary, since thecopyable iterator Python PEP standards proposal has been deferred.

Michael M.'s user avatar
Michael M.
11.2k11 gold badges22 silver badges46 bronze badges
answeredJun 19, 2011 at 6:33
ninjagecko's user avatar

Comments

588

What does theyield keyword do in Python?

Answer Outline/Summary

  • A function withyield, when called,returns aGenerator.
  • Generators are iterators because they implement theiterator protocol, so you can iterate over them.
  • A generator can also besent information, making it conceptually acoroutine.
  • In Python 3, you candelegate from one generator to another in both directions withyield from.
  • (Appendix critiques a couple of answers, including the top one, and discusses the use ofreturn in a generator.)

Generators:

yield is only legal inside of a function definition, andthe inclusion ofyield in a function definition makes it return a generator.

The idea for generators comes from other languages (see footnote 1) with varying implementations. In Python's Generators, the execution of the code isfrozen at the point of the yield. When the generator is called (methods are discussed below) execution resumes and then freezes at the next yield.

yield provides aneasy way ofimplementing the iterator protocol, defined by the following two methods:__iter__ and__next__. Both of those methodsmake an object an iterator that you could type-check with theIterator Abstract BaseClass from thecollections module.

def func():    yield 'I am'    yield 'a generator!'

Let's do some introspection:

>>> type(func)                 # A function with yield is still a function<type 'function'>>>> gen = func()>>> type(gen)                  # but it returns a generator<type 'generator'>>>> hasattr(gen, '__iter__')   # that's an iterableTrue>>> hasattr(gen, '__next__')   # and with .__next__True                           # implements the iterator protocol.

The generator type is a sub-type of iterator:

from types import GeneratorTypefrom collections.abc import Iterator>>> issubclass(GeneratorType, Iterator)True

And if necessary, we can type-check like this:

>>> isinstance(gen, GeneratorType)True>>> isinstance(gen, Iterator)True

A feature of anIteratoris that once exhausted, you can't reuse or reset it:

>>> list(gen)['I am', 'a generator!']>>> list(gen)[]

You'll have to make another if you want to use its functionality again (see footnote 2):

>>> list(func())['I am', 'a generator!']

One can yield data programmatically, for example:

def func(an_iterable):    for item in an_iterable:        yield item

The above simple generator is also equivalent to the below - as of Python 3.3 you can useyield from:

def func(an_iterable):    yield from an_iterable

However,yield from also allows for delegation to subgenerators,which will be explained in the following section on cooperative delegation with sub-coroutines.

Coroutines:

yield forms an expression that allows data to be sent into the generator (see footnote 3)

Here is an example, take note of thereceived variable, which will point to the data that is sent to the generator:

def bank_account(deposited, interest_rate):    while True:        calculated_interest = interest_rate * deposited         received = yield calculated_interest        if received:            deposited += received>>> my_account = bank_account(1000, .05)

First, we must queue up the generator with the built-in function,next. It willcall the appropriatenext or__next__ method, depending on the version ofPython you are using:

>>> first_year_interest = next(my_account)>>> first_year_interest50.0

And now we can send data into the generator. (SendingNone isthe same as callingnext.) :

>>> next_year_interest = my_account.send(first_year_interest + 1000)>>> next_year_interest102.5

Cooperative Delegation to Sub-Coroutine withyield from

Now, recall thatyield from is available in Python 3. This allows us to delegate coroutines to a subcoroutine:

def money_manager(expected_rate):    # must receive deposited value from .send():    under_management = yield                   # yield None to start.    while True:        try:            additional_investment = yield expected_rate * under_management             if additional_investment:                under_management += additional_investment        except GeneratorExit:            '''TODO: write function to send unclaimed funds to state'''            raise        finally:            '''TODO: write function to mail tax info to client'''        def investment_account(deposited, manager):    '''very simple model of an investment account that delegates to a manager'''    # must queue up manager:    next(manager)      # <- same as manager.send(None)    # This is where we send the initial deposit to the manager:    manager.send(deposited)    try:        yield from manager    except GeneratorExit:        return manager.close()  # delegate?

And now we can delegate functionality to a sub-generator and it can be usedby a generator just as above:

my_manager = money_manager(.06)my_account = investment_account(1000, my_manager)first_year_return = next(my_account) # -> 60.0

Now simulate adding another 1,000 to the account plus the return on the account (60.0):

next_year_return = my_account.send(first_year_return + 1000)next_year_return # 123.6

You can read more about the precise semantics ofyield from inPEP 380.

Other Methods: close and throw

Theclose method raisesGeneratorExit at the point the functionexecution was frozen. This will also be called by__del__ so youcan put any cleanup code where you handle theGeneratorExit:

my_account.close()

You can also throw an exception which can be handled in the generatoror propagated back to the user:

import systry:    raise ValueErrorexcept:    my_manager.throw(*sys.exc_info())

Raises:

Traceback (most recent call last):  File "<stdin>", line 4, in <module>  File "<stdin>", line 6, in money_manager  File "<stdin>", line 2, in <module>ValueError

Conclusion

I believe I have covered all aspects of the following question:

What does theyield keyword do in Python?

It turns out thatyield does a lot. I'm sure I could add even morethorough examples to this. If you want more or have some constructive criticism, let me know by commentingbelow.


Appendix:

Critique of the Top/Accepted Answer**

  • It is confused about what makes aniterable, just using a list as an example. See my references above, but in summary: aniterable has an__iter__ method returning aniterator. Aniterator additionally provides a.__next__ method, which is implicitly called byfor loops until it raisesStopIteration, and once it does raiseStopIteration, it will continue to do so.
  • It then uses a generator expression to describe what a generator is. Since a generator expression is simply a convenient way to create aniterator, it only confuses the matter, and we still have not yet gotten to theyield part.
  • InControlling a generator exhaustion he calls the.next method (which only works in Python 2), when instead he should use the built-in function,next. Callingnext(obj) would be an appropriate layer of indirection, because his code does not work in Python 3.
  • Itertools? This was not relevant to whatyield does at all.
  • No discussion of the methods thatyield provides along with the new functionalityyield from in Python 3.

The top/accepted answer is a very incomplete answer.

Critique of answer suggestingyield in a generator expression or comprehension.

The grammar currently allows any expression in a list comprehension.

expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |                     ('=' (yield_expr|testlist_star_expr))*)...yield_expr: 'yield' [yield_arg]yield_arg: 'from' test | testlist

Since yield is an expression, it has been touted by some as interesting to use it in comprehensions or generator expression - in spite of citing no particularly good use-case.

The CPython core developers arediscussing deprecating its allowance.Here's a relevant post from the mailing list:

On 30 January 2017 at 19:05, Brett Cannon wrote:

On Sun, 29 Jan 2017 at 16:39 Craig Rodrigues wrote:

I'm OK with either approach. Leaving things the way they are in Python 3is no good, IMHO.

My vote is it be a SyntaxError since you're not getting what you expect fromthe syntax.

I'd agree that's a sensible place for us to end up, as any coderelying on the current behaviour is really too clever to bemaintainable.

In terms of getting there, we'll likely want:

  • SyntaxWarning or DeprecationWarning in 3.7
  • Py3k warning in 2.7.x
  • SyntaxError in 3.8

Cheers, Nick.

-- Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia

Further, there is anoutstanding issue (10544) which seems to be pointing in the direction of thisnever being a good idea (PyPy, a Python implementation written in Python, is already raising syntax warnings.)

Bottom line, until the developers of CPython tell us otherwise:Don't putyield in a generator expression or comprehension.

Thereturn statement in a generator

InPython 3:

In a generator function, thereturn statement indicates that the generator is done and will causeStopIteration to be raised. The returned value (if any) is used as an argument to constructStopIteration and becomes theStopIteration.value attribute.

Historical note, inPython 2:"In a generator function, thereturn statement is not allowed to include anexpression_list. In that context, a barereturn indicates that the generator is done and will causeStopIteration to be raised."Anexpression_list is basically any number of expressions separated by commas - essentially, in Python 2, you can stop the generator withreturn, but you can't return a value.

Footnotes

  1. The languages CLU, Sather, and Icon were referenced in the proposalto introduce the concept of generators to Python. The general idea isthat a function can maintain an internal state and yield intermediatedata points on demand by the user. This promised to besuperior in performanceto other approaches, including Python threading, which isn't even available on some systems.

  2. This means, for example, thatrange objects aren'tIterators, even though they are iterable, because they can be reused. Like lists, their__iter__ methods return iterator objects.

  3. yield was originally introduced as a statement, meaning that itcould only appear at the beginning of a line in a code block.Nowyield creates a yield expression.https://docs.python.org/2/reference/simple_stmts.html#grammar-token-yield_stmtThis change wasproposed to allow a user to send data into the generator just asone might receive it. To send data, one must be able to assign it to something, andfor that, a statement just won't work.

user's user avatar
user
1,2401 gold badge14 silver badges31 bronze badges
answeredJun 25, 2015 at 6:11
Aaron Hall's user avatar

Comments

473

yield is just likereturn - it returns whatever you tell it to (as a generator). The difference is that the next time you call the generator, execution starts from the last call to theyield statement. Unlike return,the stack frame is not cleaned up when a yield occurs, however control is transferred back to the caller, so its state will resume the next time the function is called.

In the case of your code, the functionget_child_candidates is acting like an iterator so that when you extend your list, it adds one element at a time to the new list.

list.extend calls an iterator until it's exhausted. In the case of the code sample you posted, it would be much clearer to just return a tuple and append that to the list.

Fang's user avatar
Fang
2,4855 gold badges29 silver badges52 bronze badges
answeredOct 23, 2008 at 22:24
Douglas Mayle's user avatar

1 Comment

This is close, but not correct. Every time you call a function with a yield statement in it, it returns a brand new generator object. It's only when you call that generator's .next() method that execution resumes after the last yield.
330

There's one extra thing to mention: a function that yields doesn't actually have to terminate. I've written code like this:

def fib():    last, cur = 0, 1    while True:         yield cur        last, cur = cur, last + cur

Then I can use it in other code like this:

for f in fib():    if some_condition: break    coolfuncs(f);

It really helps simplify some problems, and makes some things easier to work with.

answeredOct 24, 2008 at 8:44
Claudiu's user avatar

Comments

318

For those who prefer a minimal working example, meditate on this interactive Python session:

>>> def f():...   yield 1...   yield 2...   yield 3... >>> g = f()>>> for i in g:...   print(i)... 123>>> for i in g:...   print(i)... >>> # Note that this time nothing was printed
Oren's user avatar
Oren
5,4795 gold badges45 silver badges68 bronze badges
answeredJan 18, 2013 at 17:25
Daniel's user avatar

Comments

300

TL;DR

Instead of this:

def square_list(n):    the_list = []                         # Replace    for x in range(n):        y = x * x        the_list.append(y)                # these    return the_list                       # lines

do this:

def square_yield(n):    for x in range(n):        y = x * x        yield y                           # with this one.

Whenever you find yourself building a list from scratch,yield each piece instead.

This was my first "aha" moment with yield.


yield is asugary way to say

build a series of stuff

Same behavior:

>>> for square in square_list(4):...     print(square)...0149>>> for square in square_yield(4):...     print(square)...0149

Different behavior:

Yield issingle-pass: you can only iterate through once. When a function has a yield in it we call it agenerator function. And aniterator is what it returns. Those terms are revealing. We lose the convenience of a container, but gain the power of a series that's computed as needed, and arbitrarily long.

Yield islazy, it puts off computation. A function with a yield in itdoesn't actually execute at all when you call it. It returns aniterator object that remembers where it left off. Each time you callnext() on the iterator (this happens in a for-loop) execution inches forward to the next yield.return raises StopIteration and ends the series (this is the natural end of a for-loop).

Yield isversatile. Data doesn't have to be stored all together, it can be made available one at a time. It can be infinite.

>>> def squares_all_of_them():...     x = 0...     while True:...         yield x * x...         x += 1...>>> squares = squares_all_of_them()>>> for _ in range(4):...     print(next(squares))...0149

If you needmultiple passes and the series isn't too long, just calllist() on it:

>>> list(square_yield(4))[0, 1, 4, 9]

Brilliant choice of the wordyield becauseboth meanings apply:

yield — produce or provide (as in agriculture)

...provide the next data in the series.

yield — give way or relinquish (as in political power)

...relinquish CPU execution until the iterator advances.

answeredMar 25, 2016 at 13:21
Bob Stein's user avatar

Comments

255

It's returning a generator. I'm not particularly familiar with Python, but I believe it's the same kind of thing asC#'s iterator blocks if you're familiar with those.

The key idea is that the compiler/interpreter/whatever does some trickery so that as far as the caller is concerned, they can keep calling next() and it will keep returning values -as if the generator method was paused. Now obviously you can't really "pause" a method, so the compiler builds a state machine for you to remember where you currently are and what the local variables etc look like. This is much easier than writing an iterator yourself.

answeredOct 23, 2008 at 22:26
Jon Skeet's user avatar

1 Comment

Actually, the methodis paused; thestate machine that keeps local variables and the “program counter” is the call frame object itself, because it lives in the heap and not in the stack.
252

Yield gives you a generator.

def get_odd_numbers(i):    return range(1, i, 2)def yield_odd_numbers(i):    for x in range(1, i, 2):       yield xfoo = get_odd_numbers(10)bar = yield_odd_numbers(10)foo[1, 3, 5, 7, 9]bar<generator object yield_odd_numbers at 0x1029c6f50>bar.next()1bar.next()3bar.next()5

As you can see, in the first casefoo holds the entire list in memory at once. It's not a big deal for a list with 5 elements, but what if you want a list of 5 million? Not only is this a huge memory eater, it also costs a lot of time to build at the time that the function is called.

In the second case,bar just gives you a generator. A generator is an iterable--which means you can use it in afor loop, etc, but each value can only be accessed once. All the values are also not stored in memory at the same time; the generator object "remembers" where it was in the looping the last time you called it--this way, if you're using an iterable to (say) count to 50 billion, you don't have to count to 50 billion all at once and store the 50 billion numbers to count through.

Again, this is a pretty contrived example, you probably would use itertools if you really wanted to count to 50 billion. :)

This is the most simple use case of generators. As you said, it can be used to write efficient permutations, using yield to push things up through the call stack instead of using some sort of stack variable. Generators can also be used for specialized tree traversal, and all manner of other things.

Andreas's user avatar
Andreas
2,52110 gold badges24 silver badges25 bronze badges
answeredJan 16, 2013 at 6:42
RBansal's user avatar

1 Comment

Just a note - in Python 3,range also returns a generator instead of a list, so you'd also see a similar idea, except that__repr__/__str__ are overridden to show a nicer result, in this caserange(1, 10, 2).
228

Here is an example in plain language. I will provide a correspondence between high-level human concepts to low-level Python concepts.

I want to operate on a sequence of numbers, but I don't want to bother myself with the creation of that sequence, I want only to focus on the operation I want to do. So, I do the following:

  • I call you and tell you that I want a sequence of numbers that are calculated in a specific way, and I let you know what the algorithm is.
    This step corresponds todefining the generator function, i.e. the function containing ayield.
  • Sometime later, I tell you, "OK, get ready to tell me the sequence of numbers".
    This step corresponds to calling the generator function which returns a generator object. Note that you don't tell me any numbers yet; you just grab your paper and pencil.
  • I ask you, "Tell me the next number", and you tell me the first number; after that, you wait for me to ask you for the next number. It's your job to remember where you were, what numbers you have already said, and what is the next number. I don't care about the details.
    This step corresponds to callingnext(generator) on the generator object.
    (In Python 2,.next was a method of the generator object; in Python 3, it is named.__next__, but the proper way to call it is using the builtinnext() function just likelen() and.__len__)
  • … repeat the previous step, until…
  • eventually, you might come to an end. You don't tell me a number; you just shout, "Hold your horses! I'm done! No more numbers!"
    This step corresponds to the generator object ending its job, and raising aStopIteration exception.
    The generator function does not need to raise the exception. It's raised automatically when the function ends or issues areturn.

This is what a generator does (a function that contains ayield); it starts executing on the firstnext(), pauses whenever it does ayield, and when asked for thenext() value it continues from the point it was last. It fits perfectly by design with the iterator protocol of Python, which describes how to sequentially request values.

The most famous user of the iterator protocol is thefor command in Python. So, whenever you do a:

for item in sequence:

it doesn't matter ifsequence is a list, a string, a dictionary or a generatorobject like described above; the result is the same: you read items off a sequence one by one.

Note thatdefining a function that contains ayield keyword is not the only way to create a generator; it's just the easiest way to create one.

For more accurate information, read aboutiterator types, theyield statement, andgenerators in the Python documentation.

user's user avatar
user
1,2401 gold badge14 silver badges31 bronze badges
answeredOct 24, 2008 at 0:36
tzot's user avatar

Comments

224

There is one type of answer that I don't feel has been given yet, among the many great answers that describe how to use generators. Here is the programming language theory answer:

Theyield statement in Python returns a generator. A generator in Python is a function that returnscontinuations (and specifically a type of coroutine, but continuations represent the more general mechanism to understand what is going on).

Continuations in programming languages theory are a much more fundamental kind of computation, but they are not often used, because they are extremely hard to reason about and also very difficult to implement. But the idea of what a continuation is, is straightforward: it is the state of a computation that has not yet finished. In this state, the current values of variables, the operations that have yet to be performed, and so on, are saved. Then at some point later in the program the continuation can be invoked, such that the program's variables are reset to that state and the operations that were saved are carried out.

Continuations, in this more general form, can be implemented in two ways. In thecall/cc way, the program's stack is literally saved and then when the continuation is invoked, the stack is restored.

In continuation passing style (CPS), continuations are just normal functions (only in languages where functions are first class) which the programmer explicitly manages and passes around to subroutines. In this style, program state is represented by closures (and the variables that happen to be encoded in them) rather than variables that reside somewhere on the stack. Functions that manage control flow accept continuation as arguments (in some variations of CPS, functions may accept multiple continuations) and manipulate control flow by invoking them by simply calling them and returning afterwards. A very simple example of continuation passing style is as follows:

def save_file(filename):  def write_file_continuation():    write_stuff_to_file(filename)  check_if_file_exists_and_user_wants_to_overwrite(write_file_continuation)

In this (very simplistic) example, the programmer saves the operation of actually writing the file into a continuation (which can potentially be a very complex operation with many details to write out), and then passes that continuation (i.e, as a first-class closure) to another operator which does some more processing, and then calls it if necessary. (I use this design pattern a lot in actual GUI programming, either because it saves me lines of code or, more importantly, to manage control flow after GUI events trigger.)

The rest of this post will, without loss of generality, conceptualize continuations as CPS, because it is a hell of a lot easier to understand and read.


Now let's talk about generators in Python. Generators are a specific subtype of continuation. Whereascontinuations are able in general to save the state of acomputation (i.e., the program's call stack),generators are only able to save the state of iteration over aniterator. Although, this definition is slightly misleading for certain use cases of generators. For instance:

def f():  while True:    yield 4

This is clearly a reasonable iterable whose behavior is well defined -- each time the generator iterates over it, it returns 4 (and does so forever). But it isn't probably the prototypical type of iterable that comes to mind when thinking of iterators (i.e.,for x in collection: do_something(x)). This example illustrates the power of generators: if anything is an iterator, a generator can save the state of its iteration.

To reiterate: Continuations can save the state of a program's stack and generators can save the state of iteration. This means that continuations are more a lot powerful than generators, but also that generators are a lot, lot easier. They are easier for the language designer to implement, and they are easier for the programmer to use (if you have some time to burn, try to read and understandthis page about continuations and call/cc).

But you could easily implement (and conceptualize) generators as a simple, specific case of continuation passing style:

Wheneveryield is called, it tells the function to return a continuation. When the function is called again, it starts from wherever it left off. So, in pseudo-pseudocode (i.e., not pseudocode, but not code) the generator'snext method is basically as follows:

class Generator():  def __init__(self,iterable,generatorfun):    self.next_continuation = lambda:generatorfun(iterable)  def next(self):    value, next_continuation = self.next_continuation()    self.next_continuation = next_continuation    return value

where theyield keyword is actually syntactic sugar for the real generator function, basically something like:

def generatorfun(iterable):  if len(iterable) == 0:    raise StopIteration  else:    return (iterable[0], lambda:generatorfun(iterable[1:]))

Remember that this is just pseudocode and the actual implementation of generators in Python is more complex. But as an exercise to understand what is going on, try to use continuation passing style to implement generator objects without use of theyield keyword.

Peter Mortensen's user avatar
Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answeredApr 4, 2013 at 14:56
aestrivex's user avatar

Comments

177

While a lot of answers show why you'd use ayield to create a generator, there are more uses foryield. It's quite easy to make a coroutine, which enables the passing of information between two blocks of code. I won't repeat any of the fine examples that have already been given about usingyield to create a generator.

To help understand what ayield does in the following code, you can use your finger to trace the cycle through any code that has ayield. Every time your finger hits theyield, you have to wait for anext or asend to be entered. When anext is called, you trace through the code until you hit theyield… the code on the right of theyield is evaluated and returned to the caller… then you wait. Whennext is called again, you perform another loop through the code. However, you'll note that in a coroutine,yield can also be used with asend… which will send a value from the callerinto the yielding function. If asend is given, thenyield receives the value sent, and spits it out the left hand side… then the trace through the code progresses until you hit theyield again (returning the value at the end, as ifnext was called).

For example:

>>> def coroutine():...     i = -1...     while True:...         i += 1...         val = (yield i)...         print("Received %s" % val)...>>> sequence = coroutine()>>> sequence.next()0>>> sequence.next()Received None1>>> sequence.send('hello')Received hello2>>> sequence.close()
answeredFeb 4, 2014 at 2:27
Mike McKerns's user avatar

1 Comment

Cute! Atrampoline (in the Lisp sense). Not often one sees those!
171

There is anotheryield use and meaning (since Python 3.3):

yield from <expr>

FromPEP 380 -- Syntax for Delegating to a Subgenerator:

A syntax is proposed for a generator to delegate part of its operations to another generator. This allows a section of code containing 'yield' to be factored out and placed in another generator. Additionally, the subgenerator is allowed to return with a value, and the value is made available to the delegating generator.

The new syntax also opens up some opportunities for optimisation when one generator re-yields values produced by another.

Moreoverthis will introduce (since Python 3.5):

async def new_coroutine(data):   ...   await blocking_action()

to avoid coroutines being confused with a regular generator (todayyield is used in both).

answeredJul 24, 2014 at 21:15
Sławomir Lenart's user avatar

Comments

162

All great answers, however a bit difficult for newbies.

I assume you have learned thereturn statement.

As an analogy,return andyield are twins.return means 'return and stop' whereas 'yield` means 'return, but continue'

  1. Try to get a num_list withreturn.
def num_list(n):    for i in range(n):        return i

Run it:

In [5]: num_list(3)Out[5]: 0

See, you get only a single number rather than a list of them.return never allows you prevail happily, just implements once and quit.

  1. There comesyield

Replacereturn withyield:

In [10]: def num_list(n):    ...:     for i in range(n):    ...:         yield i    ...:In [11]: num_list(3)Out[11]: <generator object num_list at 0x10327c990>In [12]: list(num_list(3))Out[12]: [0, 1, 2]

Now, you win to get all the numbers.

Comparing toreturn which runs once and stops,yield runs times you planed.You can interpretreturn asreturn one of them, andyield asreturn all of them. This is callediterable.

  1. One more step we can rewriteyield statement withreturn
In [15]: def num_list(n):    ...:     result = []    ...:     for i in range(n):    ...:         result.append(i)    ...:     return resultIn [16]: num_list(3)Out[16]: [0, 1, 2]

It's the core aboutyield.

The difference between a listreturn outputs and the objectyield output is:

You will always get [0, 1, 2] from a list object but only could retrieve them from 'the objectyield output' once. So, it has a new namegenerator object as displayed inOut[11]: <generator object num_list at 0x10327c990>.

In conclusion, as a metaphor to grok it:

  • return andyield are twins
  • list andgenerator are twins
answeredNov 14, 2017 at 12:02
Wizard's user avatar

1 Comment

This is understandable, but one major difference is that you can have multiple yields in a function/method. The analogy totally breaks down at that point. Yield remembers its place in a function, so the next time you call next(), your function continues on to the nextyield. This is important, I think, and should be expressed.
149

From a programming viewpoint, the iterators are implemented asthunks.

To implement concepts such as iterators, generators, concurrent execution via messages, etc., one usesmessages sent to a closure object, which has a dispatcher, and thedispatcher answers to "messages" (this concept comes from Simula and is the central part ofSmalltalk).

"next" is a message sent to a closure, created by the "iter" call.

There are lots of ways to implement this computation. I used mutation, but it is possible to do this kind of computation without mutation, by returning the current value and the next yielder (making itreferential transparent). Racket uses a sequence of transformations of the initial program in some intermediary languages, one of such rewriting making the yield operator to be transformed in some language with simpler operators.

Here is a demonstration of how yield could be rewritten, which uses the structure of R6RS, but the semantics is identical to Python's. It's the same model of computation, and only a change in syntax is required to rewrite it using yield of Python.

Welcome to Racket v6.5.0.3.-> (define gen     (lambda (l)       (define yield         (lambda ()           (if (null? l)               'END               (let ((v (car l)))                 (set! l (cdr l))                 v))))       (lambda(m)         (case m           ('yield (yield))           ('init  (lambda (data)                     (set! l data)                     'OK))))))-> (define stream (gen '(1 2 3)))-> (stream 'yield)1-> (stream 'yield)2-> (stream 'yield)3-> (stream 'yield)'END-> ((stream 'init) '(a b))'OK-> (stream 'yield)'a-> (stream 'yield)'b-> (stream 'yield)'END-> (stream 'yield)'END->
answeredAug 21, 2013 at 19:01
alinsoar's user avatar

Comments

139

Here are some Python examples of how to actually implement generators as if Python did not provide syntactic sugar for them:

As a Python generator:

from itertools import islicedef fib_gen():    a, b = 1, 1    while True:        yield a        a, b = b, a + bassert [1, 1, 2, 3, 5] == list(islice(fib_gen(), 5))

Using lexical closures instead of generators

def ftake(fnext, last):    return [fnext() for _ in xrange(last)]def fib_gen2():    #funky scope due to python2.x workaround    #for python 3.x use nonlocal    def _():        _.a, _.b = _.b, _.a + _.b        return _.a    _.a, _.b = 0, 1    return _assert [1,1,2,3,5] == ftake(fib_gen2(), 5)

Using object closures instead of generators (becauseClosuresAndObjectsAreEquivalent)

class fib_gen3:    def __init__(self):        self.a, self.b = 1, 1    def __call__(self):        r = self.a        self.a, self.b = self.b, self.a + self.b        return rassert [1,1,2,3,5] == ftake(fib_gen3(), 5)
answeredOct 3, 2012 at 20:38
Dustin Getz's user avatar

Comments

127

I was going to post "read page 19 of Beazley's 'Python: Essential Reference' for a quick description of generators", but so many others have posted good descriptions already.

Also, note thatyield can be used in coroutines as the dual of their use in generator functions. Although it isn't the same use as your code snippet,(yield) can be used as an expression in a function. When a caller sends a value to the method using thesend() method, then the coroutine will execute until the next(yield) statement is encountered.

Generators and coroutines are a cool way to set up data-flow type applications. I thought it would be worthwhile knowing about the other use of theyield statement in functions.

answeredJan 28, 2013 at 1:37
johnzachary's user avatar

Comments

110

Here is a simple example:

def isPrimeNumber(n):    print "isPrimeNumber({}) call".format(n)    if n==1:        return False    for x in range(2,n):        if n % x == 0:            return False    return Truedef primes (n=1):    while(True):        print "loop step ---------------- {}".format(n)        if isPrimeNumber(n): yield n        n += 1for n in primes():    if n> 10:break    print "writing result {}".format(n)

Output:

loop step ---------------- 1isPrimeNumber(1) callloop step ---------------- 2isPrimeNumber(2) callloop step ---------------- 3isPrimeNumber(3) callwriting result 3loop step ---------------- 4isPrimeNumber(4) callloop step ---------------- 5isPrimeNumber(5) callwriting result 5loop step ---------------- 6isPrimeNumber(6) callloop step ---------------- 7isPrimeNumber(7) callwriting result 7loop step ---------------- 8isPrimeNumber(8) callloop step ---------------- 9isPrimeNumber(9) callloop step ---------------- 10isPrimeNumber(10) callloop step ---------------- 11isPrimeNumber(11) call

I am not a Python developer, but it looks to meyield holds the position of program flow and the next loop start from "yield" position. It seems like it is waiting at that position, and just before that, returning a value outside, and next time continues to work.

It seems to be an interesting and nice ability :D

xralf's user avatar
xralf
3,79050 gold badges143 silver badges218 bronze badges
answeredDec 20, 2013 at 13:07
Engin OZTURK's user avatar

1 Comment

You are correct. But what is the effect on flow which is to see the behaviour of "yield" ? I can change the algorithm in the name of mathmatics. Will it help to get different assessment of "yield" ?
94

Here is a mental image of whatyield does.

I like to think of a thread as having a stack (even when it's not implemented that way).

When a normal function is called, it puts its local variables on the stack, does some computation, then clears the stack and returns. The values of its local variables are never seen again.

With ayield function, when its code begins to run (i.e. after the function is called, returning a generator object, whosenext() method is then invoked), it similarly puts its local variables onto the stack and computes for a while. But then, when it hits theyield statement, before clearing its part of the stack and returning, it takes a snapshot of its local variables and stores them in the generator object. It also writes down the place where it's currently up to in its code (i.e. the particularyield statement).

So it's a kind of a frozen function that the generator is hanging onto.

Whennext() is called subsequently, it retrieves the function's belongings onto the stack and re-animates it. The function continues to compute from where it left off, oblivious to the fact that it had just spent an eternity in cold storage.

Compare the following examples:

def normalFunction():    return    if False:        passdef yielderFunction():    return    if False:        yield 12

When we call the second function, it behaves very differently to the first. Theyield statement might be unreachable, but if it's present anywhere, it changes the nature of what we're dealing with.

>>> yielderFunction()<generator object yielderFunction at 0x07742D28>

CallingyielderFunction() doesn't run its code, but makes a generator out of the code. (Maybe it's a good idea to name such things with theyielder prefix for readability.)

>>> gen = yielderFunction()>>> dir(gen)['__class__', ... '__iter__',    #Returns gen itself, to make it work uniformly with containers ...            #when given to a for loop. (Containers return an iterator instead.) 'close', 'gi_code', 'gi_frame', 'gi_running', 'next',        #The method that runs the function's body. 'send', 'throw']

Thegi_code andgi_frame fields are where the frozen state is stored. Exploring them withdir(..), we can confirm that our mental model above is credible.

answeredJun 14, 2013 at 16:36
Evgeni Sergeev's user avatar

Comments

89

Imagine that you have created a remarkable machine that is capable of generating thousands and thousands of lightbulbs per day. The machine generates these lightbulbs in boxes with a unique serial number. You don't have enough space to store all of these lightbulbs at the same time, so you would like to adjust it to generate lightbulbs on-demand.

Python generators don't differ much from this concept. Imagine that you have a function calledbarcode_generator that generates unique serial numbers for the boxes. Obviously, you can have a huge number of such barcodes returned by the function, subject to the hardware (RAM) limitations. A wiser, and space efficient, option is to generate those serial numbers on-demand.

Machine's code:

def barcode_generator():    serial_number = 10000  # Initial barcode    while True:        yield serial_number        serial_number += 1barcode = barcode_generator()while True:    number_of_lightbulbs_to_generate = int(input("How many lightbulbs to generate? "))    barcodes = [next(barcode) for _ in range(number_of_lightbulbs_to_generate)]    print(barcodes)    # function_to_create_the_next_batch_of_lightbulbs(barcodes)    produce_more = input("Produce more? [Y/n]: ")    if produce_more == "n":        break

Note thenext(barcode) bit.

As you can see, we have a self-contained “function” to generate the next unique serial number each time. This function returns agenerator! As you can see, we are not calling the function each time we need a new serial number, but instead we are usingnext() given the generator to obtain the next serial number.

Lazy Iterators

To be more precise, this generator is alazy iterator! An iterator is an object that helps us traverse a sequence of objects. It's calledlazy because it does not load all the items of the sequence in memory until they are needed. The use ofnext in the previous example is theexplicit way to obtain the next item from the iterator. Theimplicit way is using for loops:

for barcode in barcode_generator():    print(barcode)

This will print barcodes infinitely, yet you will not run out of memory.

In other words, a generatorlooks like a function butbehaves like an iterator.

Real-world application?

Finally, real-world applications? They are usually useful when you work with big sequences. Imagine reading ahuge file from disk with billions of records. Reading the entire file in memory, before you can work with its content, will probably be infeasible (i.e., you will run out of memory).

answeredMar 23, 2019 at 13:55
Rafael's user avatar

Comments

84

An easy example to understand what it is:yield

def f123():    for _ in range(4):        yield 1        yield 2for i in f123():    print (i)

The output is:

1 2 1 2 1 2 1 2
ZF007's user avatar
ZF007
3,7419 gold badges33 silver badges52 bronze badges
answeredJan 2, 2017 at 12:09
Gavriel Cohen's user avatar

2 Comments

are you sure about that output? wouldnt that only be printed on a single line if you ran that print statement usingprint(i, end=' ')? Otherwise, i believe the default behavior would put each number on a new line
@user9074332, You're right, but it is written on one line to facilitate understanding
81

Like every answer suggests,yield is used for creating a sequence generator. It's used for generating some sequence dynamically. For example, while reading a file line by line on a network, you can use theyield function as follows:

def getNextLines():   while con.isOpen():       yield con.read()

You can use it in your code as follows:

for line in getNextLines():    doSomeThing(line)

Execution Control Transfer gotcha

The execution control will be transferred from getNextLines() to thefor loop when yield is executed. Thus, every time getNextLines() is invoked, execution begins from the point where it was paused last time.

Thus in short, a function with the following code

def simpleYield():    yield "first time"    yield "second time"    yield "third time"    yield "Now some useful value {}".format(12)for i in simpleYield():    print i

will print

"first time""second time""third time""Now some useful value 12"
Peter Mortensen's user avatar
Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answeredJul 29, 2015 at 6:11
Mangu Singh Rajpurohit's user avatar

Comments

80

In summary, theyield statement transforms your function into a factory that produces a special object called agenerator which wraps around the body of your original function. When thegenerator is iterated, it executes your function until it reaches the nextyield then suspends execution and evaluates to the value passed toyield. It repeats this process on each iteration until the path of execution exits the function. For instance,

def simple_generator():    yield 'one'    yield 'two'    yield 'three'for i in simple_generator():    print i

simply outputs

onetwothree

The power comes from using the generator with a loop that calculates a sequence, the generator executes the loop stopping each time to 'yield' the next result of the calculation, in this way it calculates a list on the fly, the benefit being the memory saved for especially large calculations

Say you wanted to create a your ownrange function that produces an iterable range of numbers, you could do it like so,

def myRangeNaive(i):    n = 0    range = []    while n < i:        range.append(n)        n = n + 1    return range

and use it like this;

for i in myRangeNaive(10):    print i

But this is inefficient because

  • You create an array that you only use once (this wastes memory)
  • This code actually loops over that array twice! :(

Luckily Guido and his team were generous enough to develop generators so we could just do this;

def myRangeSmart(i):    n = 0    while n < i:       yield n       n = n + 1    returnfor i in myRangeSmart(10):    print i

Now upon each iteration a function on the generator callednext() executes the function until it either reaches a 'yield' statement in which it stops and 'yields' the value or reaches the end of the function. In this case on the first call,next() executes up to the yield statement and yield 'n', on the next call it will execute the increment statement, jump back to the 'while', evaluate it, and if true, it will stop and yield 'n' again, it will continue that way until the while condition returns false and the generator jumps to the end of the function.

Peter Mortensen's user avatar
Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answeredOct 13, 2016 at 13:43
redbandit's user avatar

Comments

78

(My below answer only speaks from the perspective of using Python generator, not theunderlying implementation of generator mechanism, which involves some tricks of stack and heap manipulation.)

Whenyield is used instead of areturn in a python function, that function is turned into something special calledgenerator function. That function will return an object ofgenerator type.Theyield keyword is a flag to notify the python compiler to treat such function specially. Normal functions will terminate once some value is returned from it. But with the help of the compiler, the generator functioncan be thought of as resumable. That is, the execution context will be restored and the execution will continue from last run. Until you explicitly call return, which will raise aStopIteration exception (which is also part of the iterator protocol), or reach the end of the function. I found a lot of references aboutgenerator but thisone from thefunctional programming perspective is the most digestable.

(Now I want to talk about the rationale behindgenerator, and theiterator based on my own understanding. I hope this can help you grasp theessential motivation of iterator and generator. Such concept shows up in other languages as well such as C#.)

As I understand, when we want to process a bunch of data, we usually first store the data somewhere and then process it one by one. But thisnaive approach is problematic. If the data volume is huge, it's expensive to store them as a whole beforehand.So instead of storing thedata itself directly, why not store some kind ofmetadata indirectly, i.e.the logic how the data is computed.

There are 2 approaches to wrap such metadata.

  1. The OO approach, we wrap the metadataas a class. This is the so-callediterator who implements the iterator protocol (i.e. the__next__(), and__iter__() methods). This is also the commonly seeniterator design pattern.
  2. The functional approach, we wrap the metadataas a function. This isthe so-calledgenerator function. But under the hood, the returnedgenerator object stillIS-A iterator because it also implements the iterator protocol.

Either way, an iterator is created, i.e. some object that can give you the data you want. The OO approach may be a bit complex. Anyway, which one to use is up to you.

answeredMar 25, 2016 at 5:40
smwikipedia's user avatar

Comments

74

Yield is an object

Areturn in a function will return a single value.

If you wanta function to return a huge set of values, useyield.

More importantly,yield is abarrier.

like barrier in the CUDA language, it will not transfer control until it gets completed.

That is, it will run the code in your function from the beginning until it hitsyield. Then, it’ll return the first value of the loop.

Then, every other call will run the loop you have written in the function one more time, returning the next value until there isn't any value to return.

Peter Mortensen's user avatar
Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answeredSep 1, 2015 at 12:42
Kaleem Ullah's user avatar

Comments

71

Many people usereturn rather thanyield, but in some casesyield can be more efficient and easier to work with.

Here is an example whichyield is definitely best for:

return (in function)

import randomdef return_dates():    dates = [] # With 'return' you need to create a list then return it    for i in range(5):        date = random.choice(["1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th"])        dates.append(date)    return dates

yield (in function)

def yield_dates():    for i in range(5):        date = random.choice(["1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th"])        yield date # 'yield' makes a generator automatically which works                   # in a similar way. This is much more efficient.

Calling functions

dates_list = return_dates()print(dates_list)for i in dates_list:    print(i)dates_generator = yield_dates()print(dates_generator)for i in dates_generator:    print(i)

Both functions do the same thing, butyield uses three lines instead of five and has one less variable to worry about.

This is the result from the code:

Output

As you can see both functions do the same thing. The only difference isreturn_dates() gives a list andyield_dates() gives a generator.

A real life example would be something like reading a file line by line or if you just want to make a generator.

Peter Mortensen's user avatar
Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answeredSep 10, 2016 at 11:37
Tom Fuller's user avatar

Comments

60

Theyield keyword simply collects returning results. Think ofyield likereturn +=

ballade4op52's user avatar
ballade4op52
2,2675 gold badges29 silver badges44 bronze badges
answeredNov 18, 2015 at 19:37
Bahtiyar Özdere's user avatar

Comments

57

yield is like a return element for a function. The difference is, that theyield element turns a function into a generator. A generator behaves just like a function until something is 'yielded'. The generator stops until it is next called, and continues from exactly the same point as it started. You can get a sequence of all the 'yielded' values in one, by callinglist(generator()).

answeredMay 20, 2015 at 6:19
Will's user avatar

Comments

Protected question. To answer this question, you need to have at least 10 reputation on this site (not counting theassociation bonus). The reputation requirement helps protect this question from spam and non-answer activity.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.