Ancient use of generators
For those interested in tracking the history of generators and coroutines in Python, I just found out that PEP 342 https://www.python.org/dev/peps/pep-0342/ (which introduced send/throw/close and made "generators as coroutines" a mainstream Python concept) harks back to PEP 288 https://www.python.org/dev/peps/pep-0288/, which was rejected. PEP 288 also proposed some changes to generators. The interesting bit though is in the references: there are two links to old articles by David Mertz that describe using generators in state machines and other interesting and unconventional applications of generators. All these well predated PEP 342, so yield was a statement and could not receive a value from the function calling next() -- communication was through a shared class instance. http://gnosis.cx/publish/programming/charming_python_b5.txt http://gnosis.cx/publish/programming/charming_python_b7.txt Enjoy! -- --Guido van Rossum (python.org/~guido)
I'm glad to see that everything old is new again. All the stuff being
discussed nowadays, even up through PEP 492, was largely what I was trying
to show in 2002 .... the syntax just got nicer in the intervening 13 years
:-).
On Wed, May 6, 2015 at 10:57 AM, Guido van Rossum
For those interested in tracking the history of generators and coroutines in Python, I just found out that PEP 342 https://www.python.org/dev/peps/pep-0342/ (which introduced send/throw/close and made "generators as coroutines" a mainstream Python concept) harks back to PEP 288 https://www.python.org/dev/peps/pep-0288/, which was rejected. PEP 288 also proposed some changes to generators. The interesting bit though is in the references: there are two links to old articles by David Mertz that describe using generators in state machines and other interesting and unconventional applications of generators. All these well predated PEP 342, so yield was a statement and could not receive a value from the function calling next() -- communication was through a shared class instance.
http://gnosis.cx/publish/programming/charming_python_b5.txt http://gnosis.cx/publish/programming/charming_python_b7.txt
Enjoy!
-- --Guido van Rossum (python.org/~guido)
-- Keeping medicines from the bloodstreams of the sick; food from the bellies of the hungry; books from the hands of the uneducated; technology from the underdeveloped; and putting advocates of freedom in prisons. Intellectual property is to the 21st century what the slave trade was to the 16th.
David Beazley's tutorials on these topics are also excellent: * Generator Tricks for Systems Programmers (PyCon 2008: http://www.dabeaz.com/generators/) * A Curious Course on Coroutines and Concurrency (PyCon 2009: http://www.dabeaz.com/coroutines/) * Generators: The Final Frontier (PyCon 2014: http://www.dabeaz.com/finalgenerator/) The first one focuses on iteration, the second expands to cover PEP 342 and sending values into coroutines, while the last expands to cover *all* the different ways we use generator suspension points these days (including in context managers and asynchronous I/O). The async I/O section is particularly interesting because David initially uses threads in order to delay getting into the complexities of event loops, while still illustrating the concept of "waiting for other things to happen". (Note: once you get to Part 5 of the last tutorial, you're getting to stuff that really pushes the boundaries of what generators can do, using examples from domains that are complex in their own right. The very last section also contains some wise words around the fact that we're genuinely pushing out the boundaries of the language's expressive capabilities, which does carry some non-trivial risks) I do believe that last tutorial also does a good job of illustrating a lot of the complexity that PEP 492 is intended to *hide* from (future!) end users by simplifying the story to "use async & await for cooperative multitasking, threads & processes for preemptive multitasking, and yield & yield from for iteration", rather than having the "cooperative multitasking" case continue to be a particular way of using yield & yield from as it is in Python 3.4 (and a way of use "yield" in earlier releases). Regards, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia
participants (3)
-
David Mertz
-
Guido van Rossum
-
Nick Coghlan