473,414 Members | 1,618 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Generators versus Coroutines

It seems to me that in python, generators are not truly coroutines. I
do not understand why. What I see is that generators are used almost
exclusively for generation of lists just-in-time. Side effects are
frowned upon. Coroutines, in contrast, are like split functions where
side effects are often as important or more important than return
values. I am currently writing a real time strategy game where I have
visual effects and use generators as coroutines which yield after
processing a single frame of the effect. I can easily make an object
rotate indefinitely with a scant four or five lines of code, all of
which is in one place. So knowing that the difference between a
generator and a coroutine is minor, I come (in a very roundabout way)
to my issue. Why can I use "return" without an expression and it
implicitly returns None but I can't do the same thing with "yield" ?
Jul 18 '05 #1
8 2236
[Timothy Fitz]
It seems to me that in python, generators are not truly coroutines.
Yes, formally speaking Python generators are semi-coroutines.
I do not understand why.
Please read the PEP:

http://www.python.org/peps/pep-0255.html
What I see is that generators are used almost exclusively for
generation of lists just-in-time. Side effects are frowned upon.
Simple uses are naturally most common.
Coroutines, in contrast, are like split functions where
side effects are often as important or more important than return
values. I am currently writing a real time strategy game where I have
visual effects and use generators as coroutines which yield after
processing a single frame of the effect. I can easily make an object
rotate indefinitely with a scant four or five lines of code, all of
which is in one place. So knowing that the difference between a
generator and a coroutine is minor, I come (in a very roundabout way)
to my issue. Why can I use "return" without an expression and it
implicitly returns None
If you explicitly intend to return None as a value, it's terrible
practice to spell that as

return

instead of as

return None

It's equally terrible practice to rely on that "falling off the end"
of a Python function returns None, when you expliclty intend to return
a None value.

Plain "return" is intended to be used in Python only when
*conceptually* no value is being returned, as in "a subroutine" as
opposed to "a function". The language doesn't enforce the latter
distinction, but it's intended all the same.
but I can't do the same thing with "yield" ?


It's the purpose of "yield" to deliver a value. There was no intent
that it be possible to yield without delivering a value. If you want
to deliver the value None, then say

yield None

If you want a concept of yielding without delivering a value, that's
simply not a use case Python's generators intended to address. If you
wish, you can adhere to a *convention* that "yield None" (or "yield
False", or "yield 42", ...) means "I'm not really delivering a value".
Jul 18 '05 #2
Tim Peters <ti********@gmail.com> wrote in message
Coroutines, in contrast, are like split functions where
side effects are often as important or more important than return
values. I am currently writing a real time strategy game where I have
visual effects and use generators as coroutines which yield after
processing a single frame of the effect. I can easily make an object
rotate indefinitely with a scant four or five lines of code, all of
which is in one place. So knowing that the difference between a
generator and a coroutine is minor, I come (in a very roundabout way)
to my issue. Why can I use "return" without an expression and it
implicitly returns None
If you explicitly intend to return None as a value, it's terrible
practice to spell that as

return

instead of as

return None

It's equally terrible practice to rely on that "falling off the end"
of a Python function returns None, when you expliclty intend to return
a None value.


It was not that I want to explicitly return None, I specifically don't
want to return anything, it's just that None happens to be the default
for return so it seems like a convention to adhere to.
If you want a concept of yielding without delivering a value, that's
simply not a use case Python's generators intended to address. If you
wish, you can adhere to a *convention* that "yield None" (or "yield
False", or "yield 42", ...) means "I'm not really delivering a value".


It's not a use case, but why not? And should it be? I know generators
are semi-coroutines, but the fact is that their useage outside of
value generation is just as useful and should be adressed as such. I
do not see how accepting a plain yield would break anything at all.

I have read the PEP, twice, and I don't see why it -wasn't- addressed.
Seems to me to be a fairly large arbitrary decision.
Jul 18 '05 #3
> values. I am currently writing a real time strategy game where I have
I have written a simple 2D-real-time vehicle simulator in Python
(+ graphics library Allegro), using chained generators
which works well enough.

If your game is more ambitious, spend some time designing
a good architecture and specify your components' interfaces.
Then you could generate your code-skeleton from that;
simple generators should be sufficient to implement
your methods which are then driven by your architectures
"execution model".

ciao,
Dominic
Jul 18 '05 #4
[Tim Peters]
If you want a concept of yielding without delivering a value, that's
simply not a use case Python's generators intended to address. If
you wish, you can adhere to a *convention* that "yield None"
(or "yield False", or "yield 42", ...) means "I'm not really delivering a
value".

[Timothy Fitz] It's not a use case, but why not?
In the years generators were discussed before they were implemented,
nobody asked for that.
And should it be?
Not by my lights, no.
I know generators are semi-coroutines, but the fact is that their
useage outside of value generation is just as useful and should be
adressed as such. I do not see how accepting a plain yield would
break anything at all.
The vast majority of generator applications (as you said
before,"generators are used almost exclusively for generation of lists
just-in-time", so you already know this) have no use for that; so, for
the vast majority of generator applications, allowing it anyway would
offer nothing of value, but would reduce the quality of compile-time
error-checking.
I have read the PEP, twice, and I don't see why it -wasn't- addressed.
If someone asked for a thing, it got into the PEP. Nobody wanted it.
Seems to me to be a fairly large arbitrary decision.


For general coroutines it would have been. For Simple Generators (the
PEP's title) I think it was the right decision -- "simple" isn't
consistent with piling on gimmicks.

If you want to change it, write a new PEP.
Jul 18 '05 #5
Dominic <no****@nospam.no> wrote in message news:<cf**********@news.uni-kl.de>...
values. I am currently writing a real time strategy game where I have

I have written a simple 2D-real-time vehicle simulator in Python
(+ graphics library Allegro), using chained generators
which works well enough.

If your game is more ambitious, spend some time designing
a good architecture and specify your components' interfaces.
Then you could generate your code-skeleton from that;
simple generators should be sufficient to implement
your methods which are then driven by your architectures
"execution model".

ciao,
Dominic


It really was never an issue of "Python can't handle this." it was
more of an issue of "Python should more openly support this." I am
championing generators because they make programming SO much easier in
cases, and people really just don't use them outside if list
generation, which saddens me.
Jul 18 '05 #6
> For general coroutines it would have been. For Simple Generators (the
PEP's title) I think it was the right decision -- "simple" isn't
consistent with piling on gimmicks.

If you want to change it, write a new PEP.


Hmm, I do agree in the context of "Simple Generators" yield implying
None would not make sense and (as shown by the fact that nobody else
seems to care) is a good parse-time error facility.

Is there, then, demand for full on Coroutines? I will live having to
type those extra four letters, or maybe I'll cop out and yield 0 (hah)
but it seems other people are used to coroutines in other languages
(python is my first and only) and there might be demand.

(Also, my first post on Usenet, ever, and in under two hours I get a
respond from someone whose name I recognize on sight... I am
impressed!)
Jul 18 '05 #7
"Timothy Fitz" <fi******@gmail.com> wrote in message
news:97**************************@posting.google.c om...
Dominic <no****@nospam.no> wrote in message

news:<cf**********@news.uni-kl.de>...
values. I am currently writing a real time strategy game where I have

I have written a simple 2D-real-time vehicle simulator in Python
(+ graphics library Allegro), using chained generators
which works well enough.

If your game is more ambitious, spend some time designing
a good architecture and specify your components' interfaces.
Then you could generate your code-skeleton from that;
simple generators should be sufficient to implement
your methods which are then driven by your architectures
"execution model".

ciao,
Dominic


It really was never an issue of "Python can't handle this." it was
more of an issue of "Python should more openly support this." I am
championing generators because they make programming SO much easier in
cases, and people really just don't use them outside if list
generation, which saddens me.


Have you looked at SimPy? This is a discrete event simulation package in
pure Python, using generators in simulation objects to implement the
objects' behavior, while managing state, blocking on waits between objects,
etc. I think this may be more in the realm you are thinking, beyond simple
"list generation."

-- Paul

Jul 18 '05 #8
It really was never an issue of "Python can't handle this." it was
more of an issue of "Python should more openly support this." I am
championing generators because they make programming SO much easier in
cases, and people really just don't use them outside if list
generation, which saddens me.

Coroutines are much more powerful than Python's generators and
they can be easily abused to create unstructured
"goto"-like programs thus I assume it would still be a good idea
to put most effort into your architecture and it's
underlying execution model regardless of what you finally
use to keep state: coroutines, threads, (nested) generators.
Try to hide the implementation if you can.

Nesting generators works, but I have to admit
it's (probably) not very elegant.
I would also prefer coroutines for simulation and games as
a module similar to thread/threading.

I think Michael Jackson wrote a converter for COBOL
in the 70s, which would "invert" active entities into
ordinary functions keeping their state in ordinary data structures.
This can certainly be done in Python too.
Maybe this helps.
Ciao,
Dominic
P.S. Many programming languages do not even have
support for "generators", e.g. Java

Ha! Maybe @decorators are the solution ! ;-)
Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Dave Benjamin | last post by:
Hey all, I was just reflecting upon the old code block debate, and the thought occurred to me that Python really does have a form of code blocks in its implementation of generators. It was a...
5
by: Robert Oschler | last post by:
Preamble: - I know this is the Python forum - I know about (and have used) Jython I already posted this question in comp.lang.java. But after a week I have still not received a single reply....
15
by: Jordan Rastrick | last post by:
First, a disclaimer. I am a second year Maths and Computer Science undergraduate, and this is my first time ever on Usenet (I guess I'm part of the http generation). On top of that, I have been...
11
by: Deiter | last post by:
State Machines and Coroutines The other thread on goto: Lead me to want to ask... In the spirit of state machines and coroutines, This n00b to C would like to know if setjmp/longjmp are the only...
12
by: Wolfgang Keller | last post by:
Hello, in <dr86uc$8kt$1@wake.carmen.se>, Magnus Lycka <lycka@carmen.se> posts the result of a short test that seems to indicate that resuming a generator takes more time than calling a function....
7
by: Ben Sizer | last post by:
A simple question - can anybody give a short example of how these work and what they are good for? I've read PEP 342 and the associated bit in the What's New section and it's still all Greek to me....
0
by: Kay Schluehr | last post by:
While this has been possible before and I guess ( from somewhat superficial reading ) Michael Sparks Kamaelia project is all about this kind of stuff, Python 2.5 generators can be used trivially to...
3
by: rocco.rossi | last post by:
I would really like to know more about python 2.5's new generator characteristics that make them more powerful and analogous to coroutines. Is it possible for instance to employ them in situations...
0
by: Jean-Paul Calderone | last post by:
On Wed, 23 Apr 2008 07:17:46 -0700 (PDT), rocco.rossi@gmail.com wrote: They're not coroutines. The difference between generators in Python 2.4 and in Python 2.5 is that in Python 2.5, `yield´...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.