Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #5396
| From | "OKB (not okblacke)" <brenNOSPAMbarn@NObrenSPAMbarn.net> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: I don't understand generator.send() |
| Date | 2011-05-15 00:38 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <Xns9EE5B3899DDE8OKB@85.214.73.210> (permalink) |
| References | <1k19ubi.dwyvio12tkc4kN%see@sig.for.address> |
Victor Eijkhout wrote:
> #! /usr/bin/env python
>
> def ints():
> i=0
> while True:
> yield i
> i += 1
>
> gen = ints()
> while True:
> i = gen.next()
> print i
> if i==5:
> r = gen.send(2)
> print "return:",r
> if i>10:
> break
>
> I thought the send call would push the value "2" at the front of
> the queue. Instead it coughs up the 2, which seems senseless to me.
>
> 1/ How should I view the send call? I'm reading the manual and
> dont' get it
> 2/ Is there a way to push something in the generator object? So
> that it becomes the next yield expression? In my code I was hoping
> to get 0,1,2,3,4,5,2,6,7 as yield expressions.
You can't usefully use send() unless the generator is set up to
make use of the sent values. You can't just push values into any old
generator. For it to do anything, you need to use assign the result of
the yield to something within your generator and make use of it. See
http://docs.python.org/whatsnew/2.5.html#pep-342-new-generator-features
for an example.
--
--OKB (not okblacke)
Brendan Barnwell
"Do not follow where the path may lead. Go, instead, where there is
no path, and leave a trail."
--author unknown
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
I don't understand generator.send() see@sig.for.address (Victor Eijkhout) - 2011-05-14 19:08 -0500
Re: I don't understand generator.send() "OKB (not okblacke)" <brenNOSPAMbarn@NObrenSPAMbarn.net> - 2011-05-15 00:38 +0000
Re: I don't understand generator.send() Chris Angelico <rosuav@gmail.com> - 2011-05-15 10:47 +1000
Re: I don't understand generator.send() see@sig.for.address (Victor Eijkhout) - 2011-05-14 20:40 -0500
Re: I don't understand generator.send() Chris Rebert <crebert@ucsd.edu> - 2011-05-14 17:47 -0700
Re: I don't understand generator.send() Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-14 18:57 -0600
Re: I don't understand generator.send() Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-14 19:05 -0600
Re: I don't understand generator.send() Chris Angelico <rosuav@gmail.com> - 2011-05-15 11:17 +1000
Re: I don't understand generator.send() Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-14 21:03 -0600
csiph-web