Comments invited: PEP 279 Enhanced Generators

Nick Mathewson QnickQm at alum.mit.edu
Fri Feb 1 02:38:37 EST 2002


In article <a3deaj$b54$1 at bob.news.rcn.net>, Raymond Hettinger wrote:
> Your review and comments are invited for PEP 279:
>    http://python.sourceforge.net/peps/pep-0279.html
> 
> In summary, it proposes:
> 
> 1.  Four new built-in functions:  xmap, xfilter, xzip, and indexed

I like these a great deal; I can certainly imagine using them on a 
regular basis.

One question: why isn't "indexed" named "xindexed"?  This would 
follow the (implicit?) convention that the x-versions of functions
are lazy-evaluating, and the non-x versions evaluate greedily.

Also, one might add an "indexed" builtin along with "xindexed" for
the sake of orthogonality.  
   
BTW, I think I've noticed a bug in your code:
    In your implementation of "indexed", you probably mean to write 
   "gen.next()" rather than "collection.next()"

> 2.  Generator comprehensions:  [yield int(x) for x in floatgen()]

Strongly in favor, with the current syntax. 

> 3.  Feeding data into a generator using an optional argument to .next()

Specification notes: What happens when you try to pass an argument to
a .next() that isn't expecting one?  What happens when you call a
.next() that's expecting an argument without an argument?  This needs
to go in.

Here, I have to say I'm skeptical.  I'd like this feature, as it would
seem to enable more general-purpose control flow, but I don't have a lot
of experience with this kind of, and your example didn't convince me
it was worth the mental energy it takes me to understand this code.

Of course, this wouldn't be the first time that an ingenious construction
has gone over my head!  But to enlighten me as to why this feature is
good, it might be a good idea to cite a more fleshed-out example.  Which
other languages provide features for lazy consumers?  Who uses them?

I'd especially like to see an example of real-world code that becomes
far easier to write with the use of lazy consumers.  

> 4.  Providing a mechanism to raise exceptions in a generator

Instead of just gen.throw( ExceptionClass ), you should explicitly 
allow gen.throw( object ).  

Also, you might want to clarify whether this mechanism is only useful
in the context of lazy consumers, or whether it's handy for regular
generators as well.

Yours,

-- 
 Nick Mathewson    <Q nick Q m at alum dot mit dot edu>
                      Remove Q's to respond.  No spam.



More information about the Python-list mailing list