Generators and their next() and send() methods

Thomas Mlynarczyk thomas at
Sat Nov 15 19:00:29 CET 2008


I was playing around a bit with generators using next() and send(). And 
I was wondering why an extra send() method was introduced instead of 
simply allowing an argument for next().

Also, I find it a bit counter-intuitive that send(42) not only "sets" 
the generator to the specified value, but yields the next value at the 
same time.

As an exercise, I wanted to somehow "modify" a generator so that it 
would have a next() method accepting an argument, something like

def gen():

But I failed to come up with an implementation of the myway() function.

Any comments and/or suggestions?


Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!

More information about the Python-list mailing list