question about generators

Tim Peters tim.one at comcast.net
Wed Aug 21 22:21:08 EDT 2002


[Andrew Koenig]
> 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.

OTOH, there's every reason to believe that's exactly what it would do
<wink>.

> 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.

It's part of why generators will always be more natural in Icon, I think.
"suspend expr" in Icon doesn't have to worry about which pieces of expr are
and aren't generators, because all Icon expressions are generators.  Python
needs to draw a distinction.  There's a vaguely similar gotcha in Icon,
where, e.g., if L is an Icon list, newbies can get confused that

    suspend L

does one suspend of the list as a whole instead of suspending each list
element in turn.  The latter needs to be spelled

    suspend !L

instead, to turn "L" from an expression that generates itself into an
expression that generates its containees.  If Guido wanted to integrate
generators more deeply into Python, we could likewise say

    yield !s

to do what the proposed

    yield every s

does, and

    yield (!s,)

to do what the hypothetical

    yield (x,) for x in s

does.  I'm viewing "!" as a tightly-binding prefix operator there, markihg
"the generator" part(s!) of an expression.  Indeed, we could dispense with
"yield" in these cases too.  Double indeed, the more convenient we made
this, the more likely Guido would be to reject it both instantly and
violently <wink>.





More information about the Python-list mailing list