473,394 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Generators: section 9.10 of the python tutorial

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
3 1637
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Francis Avila | last post by:
Below is an implementation a 'flattening' recursive generator (take a nested iterator and remove all its nesting). Is this possibly general and useful enough to be included in itertools? (I know...
9
by: Francis Avila | last post by:
A little annoyed one day that I couldn't use the statefulness of generators as "resumable functions", I came across Hettinger's PEP 288 (http://www.python.org/peps/pep-0288.html, still listed as...
4
by: The_Incubator | last post by:
As the subject suggests, I am interested in using Python as a scripting language for a game that is primarily implemented in C++, and I am also interested in using generators in those scripts... ...
3
by: Carlos Ribeiro | last post by:
As a side track of my latest investigations, I began to rely heavily on generators for some stuff where I would previsouly use a more conventional approach. Whenever I need to process a list, I'm...
15
by: Jordan Rastrick | last post by:
First, a disclaimer. I am a second year Maths and Computer Science undergraduate, and this is my first time ever on Usenet (I guess I'm part of the http generation). On top of that, I have been...
3
by: Michael Sparks | last post by:
Hi, I'm posting a link to this since I hope it's of interest to people here :) I've written up the talk I gave at ACCU Python UK on the Kamaelia Framework, and it's been published as a BBC...
2
by: Tom Sheffler | last post by:
This may have been discussed before, so I apologize. Does Java have generators? I am aware of the "Iterator" interface, but it seems much more restrictive. Python generators are useful for...
0
by: Dustin J. Mitchell | last post by:
This question was first brought up in October of 2005, and was included in the "Unresolved Issues" section of my microthreading PEP, which I have quietly withdrawn from consideration due to lack of...
1
by: Gabriel Genellina | last post by:
En Mon, 05 May 2008 00:31:45 -0300, Barclay, Ken <Ken.Barclay@schwab.comescribió: No entry in the error log, on the web server? (Apache?) Perhaps you're hitting some internal limit of the cgi...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.