Generator Frustration

Jack Diederich jackdied at gmail.com
Sat Jun 4 22:06:30 EDT 2011


On Sat, Jun 4, 2011 at 9:43 PM, Gregory Ewing
<greg.ewing at canterbury.ac.nz> wrote:
> Steven D'Aprano wrote:
>
>> A nice piece of syntax that has been proposed for Python is "yield from",
>> which will do the same thing, but you can't use that yet.

You can also patch the library to always return lists instead of generators.

def seriously_I_just_want_lists(func):
  def replacement(*args, **kwargs):
    return list(func(*args, **kwargs))
  return replacement

import otherlib
otherlib.somefunc = seriously_I_just_want_lists(otherlib.somefunc)

But I'd avoid it where you can.  While the idea of a list is simpler
than the idea of an iterable (and easier to get your head around)
iterables promise less and that makes them more flexible and easier to
composite.

-Jack



More information about the Python-list mailing list