question about generators

Delaney, Timothy tdelaney at avaya.com
Wed Aug 21 05:34:43 CEST 2002


> From: Andreas.Leitgeb at siemens.at [mailto:Andreas.Leitgeb at siemens.at]
>
> There may be better words than "every", perhaps "each" sounds better
> in this context.

Whilst 'yield every' is lovely syntax sugar (or 'yield each', or whatever),
I'm starting to question the utility of it.

I post the following example of code from the sets discussion over at
python-dev (Guido's code ;) ...

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

As we can see, there are two sections of the format:

    for x in <seq>:
        yield <something>

However, in neither case would you be able to use

    yield every <seq>

I have no ideas as to whether it would be made more general. Perhaps with a
generator version of map ...

    def func (s, t=t):
        return (x,) + t

    yield every generatormap(func, subproduct)

but that is *way* uglier (and longer) than the simple for: loop.

Is it worthwhile to have a construct which can only be used in one specific
case (yield every element in an iterable object) when the alternative is so
short? The *only* advantages I can think of is that it could be somewhat
faster, and wouldn't require a temporary name.

Tim Delaney




More information about the Python-list mailing list