PEP 255: Simple Generators

Greg Ewing greg at cosc.canterbury.ac.nz
Tue Jun 26 23:56:27 EDT 2001


Paul Prescod wrote:
> 
> Others seem
> to want to teach them alongside other flow control keywords.

You might get away with teaching generators without
mentioning iterator objects if you say that they can
only be called in a special context, i.e.

   for x in g():

and that each time you hit a yield, the value gets
bound to x and the for-loop body is executed, then
execution resumes after the yield.

You'll have to emphasise that they should never
call a generator outside of such a context -- or
at least if they do, they're not allowed to come
running to the teacher for an explanation of wtf
is going on!

-- 
Greg Ewing, Computer Science Dept, University of Canterbury,	  
Christchurch, New Zealand
To get my email address, please visit my web page:	  
http://www.cosc.canterbury.ac.nz/~greg



More information about the Python-list mailing list