question about generators

Tim Peters tim.one at comcast.net
Wed Aug 14 20:53:25 EDT 2002


[Bjorn Pettersen, on Andrew Koenig's

        for i in f():
            yield i
]

> I've noticed a similar problem, ie. all my generators are recursive and
> I have to do the for-loop dance, and yes it had me flumoxed the first
> time too.
>
> It seems like this is a common enough idiom that it could warrant
> special syntax(?)

There was a lot of discussion of this at the time (dig into the iterators
mailing list archive).  We decided to keep Simple Generators, well,
*simple*, to start with.

> Coming from a C++ background (but having also programmed in Python for
> the last five years) something like
>
>   yield << f()
>
> would seem natural, and also symetric to print >> ;->

The one I like best is

    yield every some_expression

where "every" acts like a pseudo-keyword in the context of a yield statement
(like "as" does in the context of "from x import y as z").  This honors a
similar meaning for "every" in the Icon language.

Barry has already reserved

    yield << f()

to mean that the file named by "f" should read one line and suspend its
value to the caller <wink>.





More information about the Python-list mailing list