Hi Steven, Alan,

On 6 August 2013 09:44, Alan Gauld <alan.ga...@btinternet.com> wrote:

> On 06/08/13 09:26, Steven D'Aprano wrote:
>
>  All this is correct, but yield is more powerful than that. Not only does
>> yield get used to return values from a function, it also gets used to
>> send values *into* a running function.
>>
>
> Yikes. This is new to me too.
> When did that happen? Has yield always had these two way powers?
>

No, but having done a search myself about this just now, apparently since
about 2005:
http://www.python.org/dev/peps/pep-0342/



>
>  py> def cr():  # Co-Routine.
>> ...     x = yield()
>> ...     while True:
>> ...             x = yield(x + 1)
>> ...
>> py> magic = cr()
>> py> magic.send(None)
>> ()
>> py> magic.send(1)
>> 2
>>
>> Prepare to have your mind expanded
>>
>
Wow, I like you Alan was aware of the basics of yield but not send etc...,
this is rather neat.  So you can also simplistically think of "send" and
yield as akin to threaded execution, where cr() is like a thread that
blocks at the yield call after creation, and then when you call on
magic.send() the "thread" wakes up and continues and eventually returns
another value to the "calling thread" and itself blocks again at the
yield?  All without using real threads anywhere?  Very very cool....

Walter
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to