[Python-ideas] Alternative name for yield-from

Ron Adam rrr at ronadam.com
Thu Feb 19 03:41:44 CET 2009



Greg Ewing wrote:
> I've had another idea about what to call yield-from:
> 
>   y = pass g(x)

Is y a list of values sent to g?  Excuse me if I'm a bit lost there. I 
haven't played around with the new generator features yet.  I'm  thinking 
if a in "a = yield expr" is the value sent in, and 'y = pass g(x)' iterates 
all of g, before continueing, then y would have all of the values sent to 
generator g.  Or would it be the last value send to g or the value returned 
  from g?


> which means "run this generator, passing through
> any sent/yielded values etc." It's short, it's
> suggestive, it doesn't use any new keywords, and
> there's no danger of confusing it with 'yield'.

How about something like this instead:

     yield expr for next_val from g(X)

Where next_value comes from g(), and expr is the value yielded out.

Having it resemble a generator expression, makes it clearer it iterates 
until complete.



The idea of generators working as simple threads is cool, but my mental 
picture of that has probably been a bit unrealistically simple. But I'm 
still hopeful for a simple way to do it.

My wish is to be able to define a generator, and use a decorator or some 
syntax at define time to make it run in such a way as to buffer at least 
one value. That is it runs (in the back ground) until it reaches a yield 
statement instead of waiting for the .next() method to be called and 
running to the next yield statement.


One thing that always bothered me with generator functions is that they are 
not explicitly defined.  Having the presence of yield to determine if they 
are a generator or not, seems a bit implicit to me. I'd much prefer a new 
keyword, 'defgen' or something like it instead of reusing 'def'.  To me, 
generators are different enough from functions to merit their own defining 
keyword.



Or maybe it's better to define them as an object to start with?

class mygen(generator):
     ...

class mygen(threaded_generator):
     ...


I would be +1 for an objective way to define generators. (Is there a way to 
do that already?)


Cheers,
    Ron





More information about the Python-ideas mailing list