473,506 Members | 17,266 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

About that 'yield' word


def maker():
for i in range(100):
yield i
foo:4: Warning: 'yield' will become a reserved keyword in the future
File "foo", line 4
yield i
^
SyntaxError: invalid syntax

Python 2.2.2
???

Jul 18 '05 #1
5 3668
Tobiah wrote:

def maker():
for i in range(100):
yield i
foo:4: Warning: 'yield' will become a reserved keyword in the future
File "foo", line 4
yield i
^
SyntaxError: invalid syntax

Python 2.2.2
???


You seem to have omitted the necessary statement at the start
of your module (in Python 2.2):
from __future__ import generators

Alternatively, upgrade to 2.3, and you won't need that statement
any more.
Alex

Jul 18 '05 #2
Tobiah wrote:

def maker():
for i in range(100):
yield i
foo:4: Warning: 'yield' will become a reserved keyword in the future
File "foo", line 4
yield i
^
SyntaxError: invalid syntax

Python 2.2.2
???


I guess this was just thrown together as an example of "yield", rather
than real code, but:

def maker():
return range(100)

seems a little less contorted...

--
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science
Jul 18 '05 #3
|Tobiah wrote:
|> > def maker():
|> > for i in range(100):
|> > yield i

David Eppstein <ep******@ics.uci.edu> wrote previously:
|I guess this was just thrown together as an example of "yield", rather
|than real code, but:
|def maker():
| return range(100)

And less contorted still is:

xrange(100)

:-).

--
Keeping medicines from the bloodstreams of the sick; food from the bellies
of the hungry; books from the hands of the uneducated; technology from the
underdeveloped; and putting advocates of freedom in prisons. Intellectual
property is to the 21st century what the slave trade was to the 16th.

Jul 18 '05 #4
>> > def maker():
> for i in range(100):
> yield i
>
David> I guess this was just thrown together as an example of "yield",
David> rather than real code, but:

David> def maker():
David> return range(100)

David> seems a little less contorted...

Maybe so, but it behaves differently so direct comparisons aren't
particularly helpful:
def maker(): .... for i in range(100):
.... yield i
.... def maker1(): .... return range(100)
.... maker() <generator object at 0x1d80f8> maker1()

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]

--
Andrew Koenig, ar*@acm.org
Jul 18 '05 #5
In article <yu**************@tinker.research.att.com>,
Andrew Koenig <ar*@acm.org> wrote:
David> def maker():
David> return range(100)

David> seems a little less contorted...

Maybe so, but it behaves differently so direct comparisons aren't
particularly helpful:


I considered mentioning iter(range(100)) instead but I couldn't think of
an actual reason one would care about the difference.

--
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

54
6502
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
54
2661
by: Sahil Malik [MVP] | last post by:
What the heck - I can't find it. A bit shocked to see it missing though. So "Does VB.NET have the yield keyword, or any equivalent of it" ? -- - Sahil Malik Upcoming ADO.NET 2.0 book -...
2
7368
by: IntraRELY | last post by:
I know this isnt really a VB question per say, but is what I am developing in VB.NET and Excel is the only place that has provided direction. I wanted to ask the public if perhaps you could lend me...
65
4134
by: Steven Watanabe | last post by:
I know that the standard idioms for clearing a list are: (1) mylist = (2) del mylist I guess I'm not in the "slicing frame of mind", as someone put it, but can someone explain what the...
90
3365
by: John Salerno | last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part, but it does in the second? Is the first statement something different? False print 'hi' hi Thanks.
3
6909
by: Ehsan | last post by:
hi coulde any one show me the usage of "yield" keyword specially in this example: """Fibonacci sequences using generators This program is part of "Dive Into Python", a free Python book for...
10
1331
by: Dick Moores | last post by:
I'm still trying to understand classes. I've made some progress, I think, but I don't understand how to use this one. How do I call it, or any of its functions? It's from the Cookbook, at...
0
961
by: Tony Johansson | last post by:
Hello! I have noticed that I can use iterator in a method and return an IEnumerable like this public IEnumerable<TReverse() { for (int i = data.Count -1 ; i >=0 ; i--) yield return data; }
5
2279
by: Luis Zarrabeitia | last post by:
Hi there. For most use cases I think about, the iterator protocol is more than enough. However, on a few cases, I've needed some ugly hacks. Ex 1: a = iter() # assume you got the iterator...
0
7218
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7103
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
7370
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...
0
7478
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5614
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5035
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
1532
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
409
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.