473,406 Members | 2,293 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,406 software developers and data experts.

PEP 342 misunderstanding

So I've been reading up on all the new stuff in PEP 342, and trying to
understand its potential. So I'm starting with a few simple examples to
see if they work as expected, and find they dont.

I'm basically trying to do the following:

for x in range(10):
print x*2

but coroutine-style.

My initial try was:
>>def printrange():
.... for x in range(10):
.... x = yield x
.... print x
....
>>g = printrange()
for x in g:
.... g.send(x*2)
....
0
1
None
4
3
None
8
5
None
12
7
None
16
9
None
>>>
Now, I was expecting that to be 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20.

What am I missing here?

Moof

Oct 8 '06 #1
1 1369
me******@gmail.com wrote:
>>>def printrange():
... for x in range(10):
... x = yield x
... print x
...
>>>g = printrange()
for x in g:
... g.send(x*2)
...
0
1
None
4
3
None
8
5
None
12
7
None
16
9
None

Now, I was expecting that to be 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20.

What am I missing here?
the output doesn't match your sample program; I assume you left out a
print statement or two from the second loop.

what you seem to be missing is that "next" (which is called by the
for-in loop) is basically the same thing as "send(None)". in other
words, you end up pulling two items from the generator for each iteration.

</F>

Oct 8 '06 #2

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

Similar topics

14
by: Alexander May | last post by:
When I define a function in the body of a loop, why doesn't the function "close" on the loop vairable? See example below. Thanks, Alex C:\Documents and Settings\Alexander May>python Python...
15
by: Bill Mill | last post by:
Hello all, I have a misunderstanding about dynamic class methods. I don't expect this behavior: In : class test: ...: def __init__(self, method): ...: self.method = method ...:...
2
by: bardos | last post by:
Hello, I'm relatively new to C++ so I hope you can help clear up a problem. You may well be able to do so before checking the example, so don't be put off by the long post :) The problem regards...
4
by: Tim Haughton | last post by:
I think I might be misunderstanding just what the LoadXml method is doing. I have 2 seemingly identical XmlDocuments, an XPath query succeeds on one of them, and fails on the other. Can anyone tell...
3
by: Philip Tripp | last post by:
I've read numerous sources stating that view state can be disabled per control, and per page, but can't seem to keep web form controls from remembering their state on a postback. I'm using VS.Net...
12
by: Dennis | last post by:
I get the following results: Cint(&HC5798A2F) returns -981890513 as it should and Clng("&HFFFFFFFFC5798A2F") returns -981890513 as it should. However when using a string ...
6
by: Shawn Wildermuth | last post by:
I have a web project whose web.config has a ConnectionString setup. I have a second assembly with my Typed DataSets. The designer has created an App.config file with a connection string in it for...
7
by: Nathan Truhan | last post by:
All, I think I may have uncovered a bug in the IsNumeric function, or at least a misunderstanding on functionality. I am writing a Schedule Of Classes Application for our campus and have a...
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...
0
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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
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,...
0
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...

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.