[Tutor] How does this work (iterating over a function)?
steve10brink at comcast.net
steve10brink at comcast.net
Wed Jul 9 20:59:16 CEST 2014
----- Original Message -----
I guess my higher level question is: "what makes this function iterable?" and the answer appears to be the fact that it
uses a generator instead of a return function. Is that correct?
-Steve Tenbrink
----------------------------------------------------------------------
Message: 1
Date: Wed, 9 Jul 2014 16:24:46 +0100
From: Ra?l Cumplido <raulcumplido at gmail.com>
To: steve10brink at comcast.net
Cc: tutor <tutor at python.org>
Subject: Re: [Tutor] How does this work (iterating over a function)?
Message-ID:
<CAD1Rbrou1hDfxkBeJcB97361uwyNYq8tW7REk=mhUaVjeNNk2g at mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Hi,
A little bit more on this :)
Python iterator protocol will call the next() method on the iterator on
each iteration and receive the values from your iterator until a
StopIteration Exception is raised. This is how the for clause knows to
iterate. In your example below you can see this with the next example:
>>> gen = fibonacci(3)
>>> gen.next()
0
>>> gen.next()
1
>>> gen.next()
1
>>> gen.next()
2
>>> gen.next()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
>>>
Thanks,
Ra?l
--
Ra?l Cumplido
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140709/ccbbee12/attachment-0001.html>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140709/2e1040c6/attachment.html>
More information about the Tutor
mailing list