Generators: section 9.10 of the python tutorial
Peter Otten
__peter__ at web.de
Thu May 13 15:38:29 EDT 2004
More information about the Python-list mailing list
Thu May 13 15:38:29 EDT 2004
- Previous message (by thread): Generators: section 9.10 of the python tutorial
- Next message (by thread): Generators: section 9.10 of the python tutorial
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
David Stockwell wrote: > Section 9.10 of the tutorial discusses the yield keyword. When I tried > using it I get the following SyntaxError. > > What does this error mean? Does it mean we can't use yield in our code? You need the line from __future__ import generators as the first statement in your script (or you can update from Python 2.2.x to 2.3.x). > Is yield a form of a 'return' ?? In the context of a for loop, you can indeed think of yield as a kind of return that stops the execution of the function (reverse() in your example) but saves its internal state, and on the next iteration resumes execution at the point where it stopped the last time - until it encounters the next yield. When the function aka 'generater' terminates, the for loop ends. (The underlying mechanism is a bit more general, but as generators are used with for loops in the great majority of cases you shouldn't care until you are comfortable with the common case as well as classes and exceptions). Peter
- Previous message (by thread): Generators: section 9.10 of the python tutorial
- Next message (by thread): Generators: section 9.10 of the python tutorial
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list