Generator Comprehensions? was Re: a break for comprehensions

Ken Seehof kseehof at neuralintegrator.com
Wed Aug 1 01:30:21 EDT 2001


From: "Guido van Rossum" <guido at zope.com>
> > > How about if we combine your ideas:
> > >
> > > y = [yield x*2 for x in g()]
> >
> > Geeze, that's like, way obvious.  Why didn't I say that in the first
place?
> > Good stuff is always way obvious.  Nice one Paul :-)
> >
> > What do you think Guido?  => CVS?
>
> I don't think so.  Generators are most useful when they do more than
> iterate over a pre-existing sequence, so I doubt that this would be a
> common pattern.  Also I'm not sure I like overloading of the [...]
> brackets, which (to me) very strongly say "real concrete list here".
>
> --Guido van Rossum (home page: http://www.python.org/~guido/)

I'm assuming that this would be used mostly when g() is a generator,
but the sequence being iterated could be any lazy or non-lazy sequence.
In practice, this puts a filter on an existing lazy sequence.  Unfortunately
the filter is limited to a one-to-one mapping of the input sequence, but
that's true of list comprehensions too isn't it?

I think the yield keyword clearly (to me) disambiguates the expression.

Well, come to think of it, I can't think of a really good use for generator
comprehensions anyway, but you have to admit the idea is cute.  :-)

- Ken Seehof http://www.neuralintegrator.com/kseehof







More information about the Python-list mailing list