By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,050 Members | 1,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,050 IT Pros & Developers. It's quick & easy.

Generators: section 9.10 of the python tutorial

P: n/a
Hi,

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? Is
yield a form of a 'return' ??

class9.py:71: Warning: 'yield' will become a reserved keyword in the future
File "class9.py", line 71
yield data[index]
^
SyntaxError: invalid syntax

Here is the code from the tutorial: (i modifed the print statement)
def reverse(data):
for index in range(len(data)-1, -1, -1):
yield data[index]

for char in reverse('golf'):
print char,

Thanks,

David

__________________________________________________ _______________
Express yourself with the new version of MSN Messenger! Download today -
it's FREE! http://messenger.msn.com/go/onm00200471ave/direct/01/
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
David Stockwell wrote:
What does this error mean? Does it mean we can't use yield in our
code? Is yield a form of a 'return' ??

class9.py:71: Warning: 'yield' will become a reserved keyword in the future
File "class9.py", line 71
yield data[index]
^
SyntaxError: invalid syntax


Yes, you can use yield. It's different from "return", but I guess I
couldn't explain it better then the tutorial. :-)
You are probably using python2.2. If you want to create generators, you
need this line at the start of your python program:

from __future__ import generators
Jul 18 '05 #2

P: n/a
"David Stockwell" <wi*******@hotmail.com> writes:
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?


It means you're using an older version of Python in which yield isn't
a standard built-in keyword. Try putting

from __future__ import generators

at the very top of your source file to enable the yield statement.
The special import is there so that old code that uses "yield" as
(say) a variable name won't suddenly break.
Jul 18 '05 #3

P: n/a
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

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.