question about generators

Andrew Koenig ark at
Wed Aug 21 15:41:13 CEST 2002

Hmmmm... I just realized that this suggestion has a problem:

ark>     def product(s, *sets):
ark>         if not sets:
ark>             yield every [(x,) for x in s]
ark>         else:
ark>             subproduct = list(product(*sets))
ark>                 yield every [(x,) + t for x in s for t in subproduct]

There is no reason to believe that

        yield every [(x,) for x in s]

won't iterate through all of s, build up a list, then yield its elements.

And the first idea that comes to mind, namely to allow the "yield every"
syntax to incorporate list comprehensions directly

        yield every (x,) for x in s

or even

        yield (x,) for x in s

strikes me as too clever.  Oh well.

Andrew Koenig, ark at,

More information about the Python-list mailing list