Generators: section 9.10 of the python tutorial

D

David Stockwell

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/
 
M

Matteo Dell'Amico

David said:
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
 
P

Paul Rubin

David Stockwell said:
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.
 
P

Peter Otten

David said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top