Please comment on Draft PEP for Enhanced Generators
Jonathan Hogg
jonathan at onegoodidea.com
Wed Jan 30 18:42:01 EST 2002
On 30/1/2002 21:48, in article a39qb0$99f$1 at bob.news.rcn.net, "Raymond
Hettinger" <othello at javanet.com> wrote:
> And yes, it's hard to see why lazy consumers are useful until you need one.
> I'm adding a link to Dr. Mertz's article so that a clear example is
> available.
Hmmm... I read through this article, and it seems less clear than your
little example ;-) The example in it is more about generalised co-routines
than consumers.
Anyway, there's something else that's been bothering me about consumers
since I read your PEP earlier today, and I've figured out what it is that's
bothering me.
Someone please correct me if I'm wrong, but as I see it a consumer can
always be turned around into a generator used the other way. Now that Python
has nested scoping, one can easily create an in-line temporary generator and
pass it to something else to consume.
Looking at your compression example the other way around I wrote:
def compress( name, datastream ):
print 'Writing data stream to file %s ...' % name
state = []
for data in datastream:
print ' data:', data
state.append( data )
print 'Done.'
def test():
firstdat = 'Hello'
seconddat = 'World'
def _temp_():
print '<do something>'
yield firstdat
print '<do something>'
yield seconddat
compress( 'test', _temp_() )
test()
Where you had 'ostream.submit(firstdat)', in this example I can just 'yield
firstdat', and where you had 'ostream.throw( FlushStream )' I just let the
data run out. Though if it was a real exceptional condition, we could raise
an exception and it would be properly passed into the compressor.
The downside of this is that all of the "submit"s must be within a single
block - delimited by the temporary generator - so I can't pass the
compressor around and write to it from multiple scopes. However, this seems
to solve your problem without any new syntax.
Can someone suggest an example where this wouldn't be appropriate/work?
Jonathan
More information about the Python-list
mailing list