On 06/08/13 08:16, Jim Mooney wrote:
class IterableFraction(Fraction):
def __iter__(self):
yield self.numerator
yield self.denominator
Seriously, though, I thought yield was like return - one and you're
done - how are you getting two yields in there?
yield acts like a return but also a freeze frame.
So the first time you call the function it runs till it hits yield. The
next time you call the function it picks up at the yield and continues
from that point. In this case to the next yield.
The usual paradigm is to have the yield inside a loop such that every
call returns the next result of a loop. You could do that here too but
its shorter to use 2 yields:
def __iter__(self):
for value in [self.numerator, self.denominator]
yield value
hth
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor