[Tutor] How to extract numerator and denominator from fractions.Fraction(4, 32)?
Alan Gauld
alan.gauld at btinternet.com
Tue Aug 6 20:56:04 CEST 2013
On 06/08/13 18:56, Oscar Benjamin wrote:
>>> py> def cr(): # Co-Routine.
>>> ... x = yield()
>>> ... while True:
>>> ... x = yield(x + 1)
>>> ...
>>
>> I'm not keen on the argument/parameter mechanism
>> here either. Arguments are sent but not explicitly declared in the receiver,
>> that all feels rather un-pythonic to me.
>
> I'm not really sure what you mean here.
I simply meant that the syntax for send is like a normal function call:
foo.send(someValue)
But its not obvious from the code for cr() that someValue will get
assigned to x. (It's not even obvious that the cr() code would be
invoked at all!)
And in the last line I'm still not sure (I've not had time to play yet)
but I think it's saying:
"return someValue+1 and assign that result to x ready for the next
iteration of the cr() function."
At least, that's what I read it to mean. So x which looks like a regular
local variable assigned the result of yield() suddenly acquires the
value of the argument to send(). That's not intuitive and it's not
explicit (It breaks with the Python credo - explicit is better than
implicit).
It all seems a bit like too much magic going on for most beginners to
understand. And while it's true they don't have to use it, it's easy for
beginners to hit that kind of thing by accident, have their code "work"
(no errors) but actually do something very different to what they
expect. Now, in this case they have to call send(), which in itself is
not an intuitive thing to do accidentally but I shudder to think how I
would explain all that to a newbie who did stumble upon it! (Maybe by
cut n' pasting code from a forum/thread somewhere?)
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list