[Python-Dev] PEP 334 - Simple Coroutines via SuspendIteration (original) (raw)
Clark C. Evans cce at clarkevans.com
Wed Sep 8 15:26:03 CEST 2004
- Previous message: [Python-Dev] PEP 334 - Simple Coroutines via SuspendIteration
- Next message: [Python-Dev] PEP 334 - Simple Coroutines via SuspendIteration
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Wed, Sep 08, 2004 at 09:07:59AM -0400, Jp Calderone wrote: || def somefunc(): | raise SuspendIteration() | return 'foo' || def genfunc(): | yield somefunc()
Interesting, but:
- somefunc is a function, thus SuspendIteration() should terminate the function; raising an exception
- somefunc is not a generator, so it cannot be yielded.
However, perhaps something like...
def suspend(*args,**kwargs): raise SuspendIteration(*args,**kwargs) # never ever returns
def myProducer(): yeild "one" suspend() yield "two"
Regardless, this is a side point. The authors of iterators that raise a SuspendIterator() will be low-level code, like a next() which reads the next block from a socket or row from a database query. In these cases, the class style iterator is sufficient.
The real point, is that user-level generators, such as this example from the PEP (which is detailed as a class-based iterator), should transparently handle SuspendIteration() by passing it up the generator chain without killing the current scope.
| def ListAlbums(cursor):
| cursor.execute("SELECT title, artist FROM album")
| yield '
| for (title, artist) in cursor:
| yield ' 'Title Artist ' % (title, artist)
| yield ''%s %s
For those who say that this iterator should be invalidated when cursor.next() raises SuspendIteration(), I point out that it is not invalided when cursor.next() raises StopIteration().
Kind Regards,
Clark
-- Clark C. Evans Prometheus Research, LLC. http://www.prometheusresearch.com/ o office: +1.203.777.2550 ~/ , mobile: +1.203.444.0557 // (( Prometheus Research: Transforming Data Into Knowledge \ , / - Research Exchange Database /\ - Survey & Assessment Technologies ` \ - Software Tools for Researchers ~ *
- Previous message: [Python-Dev] PEP 334 - Simple Coroutines via SuspendIteration
- Next message: [Python-Dev] PEP 334 - Simple Coroutines via SuspendIteration
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]