Generator Comprehensions

Just van Rossum just at xs4all.nl
Tue Jan 29 04:55:50 EST 2002


Raymond Hettinger wrote:
> 
> Wild idea of the day:  Extend the syntax for list comprehensions
> to have an optional 'yield' to create a generator rather than a list.
> 
> sizegen = [ yield (len(line),line) for line in file.readline() ]
> print 'Line 1:', g.next()
> print 'Line 2:', g.next()

It has been proposed before (groups.google.com "yields" several
independant suggestions even). I like it, though!

It has always bothered me that the temporary variables inside list
comprehensions are still visible afterwards (and overwrite existing
variables):

   >>> [x for x in range(5)]
   [0, 1, 2, 3, 4]
   >>> x
   4
   >>> 

I wonder (and am too lazy to actually try ;-) whether it matters much
speed-wise if list comprehensions would be implemented with generators,
ie. a list comp spelled like this:

  def something(file):
      sizes = [(len(line), line) for line in file.readline()]

would be implemented like this:

  def something(file):
      def __temp_generator__():
          for line in file.readline():
              yield len(line), line
      sizes = list(__temp_generator__())

Now, if the list comprehension would start with the word "yield" it could
simply mean the list() call should be omitted...

Just



More information about the Python-list mailing list