What does the "yield" keyword do?
What is the use of the
yield keyword in Python, and what does it do?
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.
To understand what
yield does, you must understand what generators are. And before you can understand generators, you must understand 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) 1 2 3
mylist is an iterable. 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) 0 1 4
Everything you can use "
for... in..." on is an iterable;
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 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:
>>> mygenerator = (x*x for x in range(3)) >>> for i in mygenerator: ... print(i) 0 1 4
It is just the same except you used
() instead of
. BUT, you cannot perform
for 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 calculating 4, one by one.
yield is a keyword that is used like
return, except the function will return a generator.
>>> def createGenerator(): ... mylist = range(3) ... for i in mylist: ... yield i*i ... >>> mygenerator = createGenerator() # create a generator >>> print(mygenerator) # mygenerator is an object! <generator object createGenerator at 0xb7555c34> >>> for i in mygenerator: ... 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.
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
Your code explained
# Here you create the method of the node object that will return the generator def _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 is no more than two values: the left and the right children
# Create an empty list and a list with the current object reference result, 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 distance is ok, then you can fill the result if distance <= max_dist and distance >= min_dist: result.extend(node._values) # Add the children of the candidate in the candidate's list # so the loop will keep running until it will have 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))exhaust all the values of the generator, but
whilekeeps creating new generator objects which will produce different values from the previous ones since it's not applied on the same node.
extend()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:
- You don't need to read the values twice.
- 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, use
It can be useful for various things like controlling access to a resource.
Itertools, your best friend
The itertools 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?
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
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 about how
for loops work.
Read more... Read less...
Shortcut to understanding
When you see a function with
yield statements, apply this easy trick to understand what will happen:
- Insert a line
result = at the start of the function.
- Replace each
- Insert a line
return resultat the bottom of the function.
- Yay - no more
yieldstatements! Read and figure out code.
- Compare function to the original definition.
This trick may give you an idea of the logic behind the function, but what actually happens with
yield 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, the iterator protocol - when you write
for x in mylist: ...loop body...
Python performs the following two steps:
Gets an iterator for
iter(mylist)-> this returns an object with a
__next__()in Python 3).
[This is the step most people forget to tell you about]
Uses the iterator to loop over items:
Keep calling the
next()method on the iterator returned from step 1. The return value from
next()is assigned to
xand the loop body is executed. If an exception
StopIterationis raised from within
next(), 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 to loop over the contents of an object - so it could be a for loop, but it could also be code like
otherlist is a Python list).
mylist is an iterable 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 an iterator. An iterator is an object with a
next() method. It is possible to implement both
next() on the same class, and have
self. 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:
- Built-in lists, dictionaries, tuples, sets, files.
- User-defined classes that implement
Note that a
for 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 calls
next(). Built-in lists return their items one by one, dictionaries return the keys one by one, files return the lines one by one, etc. And generators return... well that's where
yield comes in:
def f123(): yield 1 yield 2 yield 3 for item in f123(): print item
yield statements, if you had three
return statements in
f123() only the first would get executed, and the function would exit. But
f123() is no ordinary function. When
f123() is called, it does 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 the
for loop tries to loop over the generator object, the function resumes from its suspended state at the very next line after the
yield it previously returned from, executes the next line of code, in this case, a
yield statement, and returns that as the next item. This happens until the function exits, at which point the generator raises
StopIteration, and the loop exits.
So the generator object is sort of like an adapter - at one end it exhibits the iterator protocol, by exposing
next() methods to keep the
for 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 class SomethingIter that keeps the state in instance members and performs the next logical step in it's
__next__() in Python 3) method. Depending on the logic, the code inside the
next() method may end up looking very complex and be prone to bugs. Here generators provide a clean and easy solution.
Think of it this way:
An iterator is just a fancy sounding term for an object that has a
next() method. So a yield-ed function ends up being something like this:
def some_function(): for i in xrange(4): yield i for 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 StopIteration def some_func(): return it() for i in some_func(): print i
For more insight as to what's happening behind the scenes, the
for 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 this is an oversimplification for illustrative purposes. :)
yield keyword is reduced to two simple facts:
- If the compiler detects the
yieldkeyword anywhere inside a function, that function no longer returns via the
returnstatement. Instead, it immediately returns a lazy "pending list" object called a generator
- A generator is iterable. What is an iterable? It's anything like a
rangeor dict-view, with a built-in protocol for visiting each element in a certain order.
In a nutshell: a generator is a lazy, incrementally-pending list, and
yield statements allow you to use function notation to program the list values the generator should incrementally spit out.
generator = myYieldingFunction(...) x = list(generator) generator v [x, ..., ???] generator v [x, x, ..., ???] generator v [x, x, x, ..., ???] StopIteration exception [x, x, x] done list==[x, x, x]
Let's define a function
makeRange that's just like Python's
makeRange(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 into
list() (just like you could any iterable):
>>> list(makeRange(5)) [0, 1, 2, 3, 4]
Comparing example to "just returning a list"
The above example can be thought of as merely creating a list which you append to and return:
# list-version # # generator-version def 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 ## indented 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 the
itertools module (be sure to use
chain.from_iterable rather than
chain when warranted). For example, you might even use generators to implement infinitely-long lazy lists like
itertools.count(). You could implement your own
def enumerate(iterable): zip(count(), iterable), or alternatively do so with the
yield keyword in a while-loop.
Please note: generators can actually be used for many more things, such as implementing coroutines or non-deterministic programming or 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 do
list(makeRange(5)). This is what I describe earlier as a "lazy, incremental list".
>>> x=iter(range(5)) >>> next(x) 0 >>> 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 function
next() just calls the objects
.next() function, which is a part of the "iteration protocol" and is found on all iterators. You can manually use the
next() function (and other parts of the iteration protocol) to implement fancy things, usually at the expense of readability, so try to avoid doing that...
Normally, most people would not care about the following distinctions and probably want to stop reading here.
In Python-speak, an iterable is any object which "understands the concept of a for-loop" like a list
[1,2,3], and an iterator is a specific instance of the requested for-loop like
[1,2,3].__iter__(). A generator 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 an iterator; that is, it is one-time-use. If you want to reuse it, you should call
myRange(...) again. If you need to use the result twice, convert the result to a list and store it in a variable
x = list(myRange(5)). Those who absolutely need to clone a generator (for example, who are doing terrifyingly hackish metaprogramming) can use
itertools.tee if absolutely necessary, since the copyable iterator Python PEP standards proposal has been deferred.
What does the
yieldkeyword do in Python?
- A function with
yield, when called, returns a Generator.
- Generators are iterators because they implement the iterator protocol, so you can iterate over them.
- A generator can also be sent information, making it conceptually a coroutine.
- In Python 3, you can delegate from one generator to another in both directions with
- (Appendix critiques a couple of answers, including the top one, and discusses the use of
returnin a generator.)
yield is only legal inside of a function definition, and the inclusion of
yield 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 is frozen 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 an
easy way of implementing the iterator protocol, defined by the following two methods:
next (Python 2) or
__next__ (Python 3). Both of those methods
make an object an iterator that you could type-check with the
Iterator Abstract Base
Class from the
>>> def func(): ... yield 'I am' ... yield 'a generator!' ... >>> 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 iterable True >>> hasattr(gen, 'next') # and with .next (.__next__ in Python 3) True # implements the iterator protocol.
The generator type is a sub-type of iterator:
>>> import collections, types >>> issubclass(types.GeneratorType, collections.Iterator) True
And if necessary, we can type-check like this:
>>> isinstance(gen, types.GeneratorType) True >>> isinstance(gen, collections.Iterator) True
A feature of an
Iterator is 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 (and not available in Python 2), you can use
def func(an_iterable): yield from an_iterable
yield from also allows for delegation to subgenerators,
which will be explained in the following section on cooperative delegation with sub-coroutines.
yield forms an expression that allows data to be sent into the generator (see footnote 3)
Here is an example, take note of the
received 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 builtin function,
next. It will
call the appropriate
__next__ method, depending on the version of
Python you are using:
>>> first_year_interest = next(my_account) >>> first_year_interest 50.0
And now we can send data into the generator. (Sending
the same as calling
>>> next_year_interest = my_account.send(first_year_interest + 1000) >>> next_year_interest 102.5
Cooperative Delegation to Sub-Coroutine with
Now, recall that
yield 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 used by 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 of
yield from in PEP 380.
Other Methods: close and throw
close method raises
GeneratorExit at the point the function
execution was frozen. This will also be called by
__del__ so you
can put any cleanup code where you handle the
You can also throw an exception which can be handled in the generator or propagated back to the user:
import sys try: raise ValueError except: my_manager.throw(*sys.exc_info())
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
I believe I have covered all aspects of the following question:
What does the
yieldkeyword do in Python?
It turns out that
yield does a lot. I'm sure I could add even more
thorough examples to this. If you want more or have some constructive criticism, let me know by commenting
Critique of the Top/Accepted Answer**
- It is confused on what makes an iterable, just using a list as an example. See my references above, but in summary: an iterable has an
__iter__method returning an iterator. An iterator provides a
.next(Python 2 or
.__next__(Python 3) method, which is implicitly called by
forloops until it raises
StopIteration, and once it does, it will continue to do so.
- It then uses a generator expression to describe what a generator is. Since a generator is simply a convenient way to create an iterator, it only confuses the matter, and we still have not yet gotten to the
- In Controlling a generator exhaustion he calls the
.nextmethod, when instead he should use the builtin function,
next. It would be an appropriate layer of indirection, because his code does not work in Python 3.
- Itertools? This was not relevant to what
yielddoes at all.
- No discussion of the methods that
yieldprovides along with the new functionality
yield fromin Python 3. The top/accepted answer is a very incomplete answer.
Critique of answer suggesting
yield 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 are discussing 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 3 is no good, IMHO.
My vote is it be a SyntaxError since you're not getting what you expect from the syntax.
I'd agree that's a sensible place for us to end up, as any code relying on the current behaviour is really too clever to be maintainable.
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
-- Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
Further, there is an outstanding issue (10544) which seems to be pointing in the direction of this never 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 put
yield in a generator expression or comprehension.
return statement in a generator
In Python 2:
In a generator function, the
returnstatement is not allowed to include an
expression_list. In that context, a bare
returnindicates that the generator is done and will cause
StopIterationto be raised.
expression_list is basically any number of expressions separated by commas - essentially, in Python 2, you can stop the generator with
return, but you can't return a value.
In Python 3:
In a generator function, the
returnstatement indicates that the generator is done and will cause
StopIterationto be raised. The returned value (if any) is used as an argument to construct
StopIterationand becomes the
The languages CLU, Sather, and Icon were referenced in the proposal to introduce the concept of generators to Python. The general idea is that a function can maintain internal state and yield intermediate data points on demand by the user. This promised to be superior in performance to other approaches, including Python threading, which isn't even available on some systems.
This means, for example, that
rangein Python 3) aren't
Iterators, even though they are iterable, because they can be reused. Like lists, their
__iter__methods return iterator objects.
yieldwas originally introduced as a statement, meaning that it could only appear at the beginning of a line in a code block. Now
yieldcreates a yield expression. https://docs.python.org/2/reference/simple_stmts.html#grammar-token-yield_stmt This change was proposed to allow a user to send data into the generator just as one might receive it. To send data, one must be able to assign it to something, and for that, a statement just won't work.
yield is just like
return - 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 the
yield 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 function
get_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.