[Python-Dev] PEP 334 - Simple Coroutines via SuspendIteration

Clark C. Evans cce at clarkevans.com
Wed Sep 8 15:32:15 CEST 2004


On Wed, Sep 08, 2004 at 09:26:03AM -0400, Clark C. Evans wrote:
| 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.

It's too early for me to be posting; scrap the nonsense in this second
point.  I don't think this changes the suggestion below though.

| 
| 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 '<html><body><table><tr><td>Title</td><td>Artist</td></tr>'
| |         for (title, artist) in cursor:
| |             yield '<tr><td>%s</td><td>%s</td></tr>' % (title, artist)
| |         yield '</table></body></html>'
| 
| 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
|     ~ *
| _______________________________________________
| Python-Dev mailing list
| Python-Dev at python.org
| http://mail.python.org/mailman/listinfo/python-dev
| Unsubscribe: http://mail.python.org/mailman/options/python-dev/cce%40clarkevans.com

-- 
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
    ~ *


More information about the Python-Dev mailing list