[Tutor] How to extract numerator and denominator from fractions.Fraction(4, 32)?

Walter Prins wprins at gmail.com
Tue Aug 6 11:01:55 CEST 2013


Hi Steven, Alan,

On 6 August 2013 09:44, Alan Gauld <alan.gauld at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130806/5831e9c4/attachment.html>


More information about the Tutor mailing list