Что такое yield в python
In this article, we will cover the yield keyword in Python. Before starting, let’s understand the yield keyword definition.
Syntax of the Yield Keyword in Python
What does the Yield Keyword do?
yield keyword is used to create a generator function. A type of function that is memory efficient and can be used like an iterator object.
In layman terms, the yield keyword will turn any expression that is given with it into a generator object and return it to the caller. Therefore, you must iterate over the generator object if you wish to obtain the values stored there. we will see the yield python example.
Difference between return and yield Python
The yield keyword in Python is similar to a return statement used for returning values in Python which returns a generator object to the one who calls the function which contains yield, instead of simply returning a value. The main difference between them is, the return statement terminates the execution of the function. Whereas, the yield statement only pauses the execution of the function. Another difference is return statements are never executed. whereas, yield statements are executed when the function resumes its execution.
Advantages of yield:
- Using yield keyword is highly memory efficient, since the execution happens only when the caller iterates over the object.
- As the variables states are saved, we can pause and resume from the same point, thus saving time.
Disadvantages of yield:
- Sometimes it becomes hard to understand the flow of code due to multiple times of value return from the function generator.
- Calling of generator functions must be handled properly, else might cause errors in program.
Example 1: Generator functions and yield Keyword in Python
Generator functions behave and look just like normal functions, but with one defining characteristic. Instead of returning data, Python generator functions use the yield keyword. Generators’ main benefit is that they automatically create the functions __iter__() and next (). Generators offer a very tidy technique to produce data that is enormous or limitless.
Понимание yield в Python

Ключевое слово yield в Python используется для создания генераторов. Генератор – это коллекция, которая продуцирует элементы на лету и может быть повторена только один раз. С помощью генераторов можно повысить производительность приложения и снизить потребление памяти по сравнению с обычными коллекциями.
В этой статье будет рассказано, как использовать ключевое слово yield в Python и как именно оно работает. Но сначала давайте поймём разницу между простым списком и генератором, а затем посмотрим, как yield можно использоваться для создания более сложных генераторов.
Различия между списком и генератором
В следующих примерах будет создан список и генератор, чтобы увидеть их отличия. Сначала создадим простой список и проверим его тип:
При запуске этого кода, программа вернёт результат «list». Теперь давайте переберём все элементы в списке squared_list.
Приведенный выше сценарий вернёт следующие результаты:
Теперь создадим генератор и выполнить ту же задачу:
Генератор создаётся подобно коллекции списка, но вместо квадратных скобок нужно использовать круглые скобки. Приведенный выше сценарий вернёт значение «generator» как тип переменной squared_gen. Теперь давайте переберём элементы генератора с помощью цикла for.
Выходные данные такие же, как у списка. Так в чем же разница? Одно из главных отличий заключается в том, как в список и генератор хранят элементы в памяти. Списки хранят все элементы в памяти сразу, тогда как генераторы «создают» каждый элемент на лету, отображая их, а затем перемещаются к следующему элементу, удаляя предыдущий элемент из памяти.
Один из способов проверить это, узнать длину как списка, так и генератора, который только что создали. Функции len(squared_list) вернет 5, а len(squared_gen) выдаст ошибку отсутствия длины у генератора. Кроме того, список можно перебирать столько раз, сколько захотите, но генератор можно перебирать только один раз. Для повторной итерации необходимо создать генератор снова.
Использование ключевого слова yield
Теперь мы знаем разницу между простыми коллекциями и генераторами, давайте посмотрим, как yield может помочь нам определить генератор.
В предыдущих примерах был создан генератор неявно, используя синтаксис генераторов списков. Однако в более сложных сценариях необходимо создавать функции, которые возвращают генератор. Ключевое слово yield, в отличие от оператора return, используется для превращения обычной функции Python в генератор. Оно используется в качестве альтернативы одновременному возвращению целого списка.
Опять же, давайте сначала посмотрим, что возвращает наша функция, если не использовать ключевое слово yield. Выполните следующий сценарий:
В этом скрипте создается функция cube_numbers, которая принимает список чисел, вычисляет их куб и возвращает вызывающему объекту список целиком. При вызове этой функции список кубов возвращается и сохраняется в переменную cubes. Как видно из вывода, возвращаемые данные – это список целиком:
Теперь, изменим сценарий, так чтобы он возвращал генератор.
В приведенном выше скрипте функция cube_numbers возвращает генератор вместо списка кубов чисел. Создать генератор с помощью ключевого слова yield очень просто. Здесь нам не нужна временная переменная cube_list для хранения куба числа, поэтому даже наш метод cube_numbers проще. Кроме того, не используется оператор return, но вместо него используется слово yield для возвращения куба числа внутри цикла.
Теперь, когда функция cube_number возвращает генератор, проверим его, запустив код:
Несмотря на то, что был произведён вызов функции cube_numbers, она фактически не выполняется на данный момент времени, и в памяти еще нет элементов.
Получение значение из генератора:
Вышеуказанная функция возвратит «1». Теперь, когда снова вызывается next генератора, функция cube_numbers возобновит выполнение с того места, где она ранее остановилась на yield. Функция будет продолжать выполняться до тех пор, пока снова не найдет yield. Следующая функция будет продолжать возвращать значение куба по одному, пока все значения в списке не будут проитерированы.
Как только все значения будут проитерированы, следующий вызов функции создаст исключение StopIteration. Важно отметить, что генератор кубов не хранит какие-либо элементы в памяти, а значения в кубе вычисляются во время выполнения, возвращаются и забываются. Используется только дополнительная память для хранения данных состояния самого генератора, которая, как правило, гораздо меньше, чем полный список. Это делает генераторы идеально подходящими для ресурсоемких задач.
Вместо того, чтобы использовать next итератора, можно также использовать цикл for для перебора значений генераторов. При использовании цикла for за кулисами вызывается next итерации, пока не будут возвращены все элементы генератора.
Оптимизация производительности
Как упоминалось ранее, генераторы очень удобны, когда дело доходит до задач, активно расходующих память, так как они не хранят все элементы коллекции в памяти, а генерируют элементы на лету и удаляют их, как только итератор переходит к следующему элементу.
В предыдущих примерах разница в производительности простого списка и генератора не была видна, так как размеры списка были малы. В этом разделе рассмотрим некоторые примеры, где можно сравнить производительность списков и генераторов.
В приведенном ниже коде представлена функция, которая возвращает список, содержащий 1 миллион фиктивных объектов car. Рассчитаем память, процессорное время до и после вызова функции.
Взглянем на следующий код:
Примечание: возможно, придется выполнить pip install psutil, чтобы этот код был работоспособен.
На компьютере автора статьи получены следующие результаты (в вашем случае результаты могут быть иными):
До создания списка потребляемая память процесса составляла 8 МБ, а после создания списка с 1 миллионом элементов занимаемая память подскочила до 334 МБ. Кроме того, для создания списка было затрачено 1.58 секунды.
Теперь, повторим процесс, но заменив список на генератор. Выполним следующий сценарий:
Результаты при выполнении вышеуказанного скрипта:
Из выходных данных видно, что при использовании генераторов разница в потреблении памяти незначительна (она остается на уровне 8 МБ), так как генераторы не хранят элементы в памяти. Кроме того, время, затраченное на вызов функции генератора, составило всего 0,000003 секунды – это намного меньше затраченного времени, по сравнению с списком.
Вывод
Надеюсь, после прочтения этой статьи вы лучше стали понимать ключевое слово yield, в том числе, как его использовать, для чего оно используется. Генераторы Python – отличный способ улучшить производительность программ, и они очень просты в использовании, но понимание того, когда их использовать, является проблемой для многих начинающих программистов.
What does the "yield" keyword do in Python?
What is the use of the yield keyword in Python? What does it do?
For example, I’m trying to understand this code 1 :
And this is the caller:
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.
![]()
![]()
51 Answers 51
To understand what yield does, you must understand what generators are. And before you can understand generators, you must understand iterables.
Iterables
When you create a list, you can read its items one by one. Reading its items one by one is called iteration:
mylist is an iterable. When you use a list comprehension, you create a list, and so an iterable:
Everything you can use " for. in. " on is an iterable; lists , strings , files.
These iterables are handy because you can read them as much as you wish, but you store all the values in memory and this is not always what you want when you have a lot of values.
Generators
Generators are iterators, a kind of iterable you can only iterate over once. Generators do not store all the values in memory, they generate the values on the fly:
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
yield is a keyword that is used like return , except the function will return a generator.
Here it’s a useless example, but it’s handy when you know your function will return a huge set of values that you will only need to read once.
To master yield , you must understand that when you call the function, the code you have written in the function body does not run. The function only returns the generator object, this is a bit tricky.
Then, your code will continue from where it left off each time for uses the generator.
Now the hard part:
The first time the for calls the generator object created from your function, it will run the code in your function from the beginning until it hits yield , then it’ll return the first value of the loop. Then, each subsequent call will run another iteration of the loop you have written in the function and return the next value. This will continue until the generator is considered empty, which happens when the function runs without hitting yield . That can be because the loop has come to an end, or because you no longer satisfy an "if/else" .
Your code explained
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, but while keeps creating new generator objects which will produce different values from the previous ones since it’s not applied on the same node.
The 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:
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
Note: For Python 3, use print(corner_street_atm.__next__()) or print(next(corner_street_atm))
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?
Then just import itertools .
An example? Let’s see the possible orders of arrival for a four-horse race:
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 about how for loops work.
Shortcut to understanding yield
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 yield expr with result.append(expr) .
- Insert a line return result at the bottom of the function.
- Yay — no more yield statements! Read and figure out the 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
Python performs the following two steps:
Gets an iterator for mylist :
Call iter(mylist) -> this returns an object with a next() method (or __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 x and the loop body is executed. If an exception StopIteration is 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.extend(mylist) (where otherlist is a Python list).
Here 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 __iter__() and next() on the same class, and have __iter__() return 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, and files.
- User-defined classes that implement __iter__() .
- Generators.
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:
Instead of 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 __iter__() and 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 its next() (or __next__() in Python 3) method. Depending on the logic, the code inside the next() method may end up looking very complex and 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:
This is basically what the Python interpreter does with the above code:
For more insight as to what’s happening behind the scenes, the for loop can be rewritten to this:
Does that make more sense or just confuse you more? 🙂
I should note that this is an oversimplification for illustrative purposes. 🙂
The yield keyword is reduced to two simple facts:
- If the compiler detects the yield keyword anywhere inside a function, that function no longer returns via the return statement. 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 list or set or range or dict-view, with a built-in protocol for visiting each element in a certain order.
In a nutshell: Most commonly, 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. Furthermore, advanced usage lets you use generators as coroutines (see below).
Basically, whenever the yield 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 calls next() and catches a StopIteration exception, etc.). You might have encountered generators with generator 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 function makeRange that’s just like Python’s range . Calling makeRange(n) RETURNS A GENERATOR:
To force the generator to immediately return its pending values, you can pass it into list() (just like you could any iterable):
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:
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:
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".
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.
Coroutines
A coroutine (generators which generally accept input via the yield keyword e.g. nextInput = yield nextOutput , as a form of two-way communication) is basically a computation which 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 a yield keyword), the computation is paused and control is inverted (yielded) back to the ‘calling’ function (the frame which requested the next 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 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, 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.
. 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 (still works in Python 3) if absolutely necessary, since the copyable iterator Python PEP standards proposal has been deferred.
What does the yield keyword do in Python?
Answer Outline/Summary
- 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 yield from .
- (Appendix critiques a couple of answers, including the top one, and discusses the use of return in a generator.)
Generators:
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: __iter__ and __next__ . Both of those methods make an object an iterator that you could type-check with the Iterator Abstract Base Class from the collections module.
Let’s do some introspection:
The generator type is a sub-type of iterator:
And if necessary, we can type-check like this:
A feature of an Iterator is that once exhausted, you can’t reuse or reset it:
You’ll have to make another if you want to use its functionality again (see footnote 2):
One can yield data programmatically, for example:
The above simple generator is also equivalent to the below — as of Python 3.3 you can use yield from :
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 the received variable, which will point to the data that is sent to the generator:
First, we must queue up the generator with the built-in function, next . It will call the appropriate next or __next__ method, depending on the version of Python you are using:
And now we can send data into the generator. (Sending None is the same as calling next .) :
Cooperative Delegation to Sub-Coroutine with yield from
Now, recall that yield from is available in Python 3. This allows us to delegate coroutines to a subcoroutine:
And now we can delegate functionality to a sub-generator and it can be used by a generator just as above:
Now simulate adding another 1,000 to the account plus the return on the account (60.0):
You can read more about the precise semantics of yield from in PEP 380.
Other Methods: close and throw
The 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 GeneratorExit :
You can also throw an exception which can be handled in the generator or propagated back to the user:
Conclusion
I believe I have covered all aspects of the following question:
What does the yield keyword 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 below.
Appendix:
Critique of the Top/Accepted Answer**
- It is confused about 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 additionally provides a .__next__ method, which is implicitly called by for loops until it raises StopIteration , and once it does raise StopIteration , 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 an iterator, it only confuses the matter, and we still have not yet gotten to the yield part.
- In Controlling a generator exhaustion he calls the .next method (which only works in Python 2), when instead he should use the built-in function, next . Calling next(obj) would be an appropriate layer of indirection, because his code does not work in Python 3.
- Itertools? This was not relevant to what yield does at all.
- No discussion of the methods that yield provides along with the new functionality yield from in 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.
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.
The return statement in a generator
In a generator function, the return statement indicates that the generator is done and will cause StopIteration to be raised. The returned value (if any) is used as an argument to construct StopIteration and becomes the StopIteration.value attribute.
Historical note, in Python 2: "In a generator function, the return statement is not allowed to include an expression_list . In that context, a bare return indicates that the generator is done and will cause StopIteration to be raised." An 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.
Footnotes
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 an 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 range objects aren’t Iterator s, even though they are iterable, because they can be reused. Like lists, their __iter__ methods return iterator objects.
yield was originally introduced as a statement, meaning that it could only appear at the beginning of a line in a code block. Now yield creates 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.
![]()
There’s one extra thing to mention: a function that yields doesn’t actually have to terminate. I’ve written code like this:
Then I can use it in other code like this:
It really helps simplify some problems, and makes some things easier to work with.
For those who prefer a minimal working example, meditate on this interactive Python session:
TL;DR
Instead of this:
do this:
Whenever you find yourself building a list from scratch, yield each piece instead.
This was my first «aha» moment with yield.
yield is a sugary way to say
Yield is single-pass: you can only iterate through once. When a function has a yield in it we call it a generator function. And an iterator 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 is lazy, it puts off computation. A function with a yield in it doesn’t actually execute at all when you call it. It returns an iterator object that remembers where it left off. Each time you call next() 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 is versatile. Data doesn’t have to be stored all together, it can be made available one at a time. It can be infinite.
If you need multiple passes and the series isn’t too long, just call list() on it:
Brilliant choice of the word yield because both 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.
![]()
Yield gives you a generator.
As you can see, in the first case foo 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 a for 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.
It’s returning a generator. I’m not particularly familiar with Python, but I believe it’s the same kind of thing as C#’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.
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:
The yield statement in Python returns a generator. A generator in Python is a function that returns continuations (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 the call/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:
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. Whereas continuations are able in general to save the state of a computation (i.e., the program’s call stack), generators are only able to save the state of iteration over an iterator. Although, this definition is slightly misleading for certain use cases of generators. For instance:
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 understand this page about continuations and call/cc).
But you could easily implement (and conceptualize) generators as a simple, specific case of continuation passing style:
Whenever yield 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’s next method is basically as follows:
where the yield keyword is actually syntactic sugar for the real generator function, basically something like:
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 the yield keyword.