Created
January 25, 2015 02:07
-
-
Save sujayy1983/e5f8a927b413b16bec03 to your computer and use it in GitHub Desktop.
1. My iterator implementation. 2. Using the iterator in two ways.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
@Author: Sujayyendhiren Ramrarao Srinivasamurthi | |
@Description: My own iterator. | |
""" | |
class Myfib: | |
""" Fibonacci class""" | |
def __init__(self, max): | |
self.max = max | |
def __iter__(self): | |
""" The iterator method implementation makes this class iterable""" | |
self.a = 0 | |
self.b = 1 | |
return self | |
def next(self): | |
"""Method that returns the next value in the sequence""" | |
fib = self.a | |
if fib > self.max: | |
raise StopIteration | |
self.a, self.b = self.b, self.a + self.b | |
return fib | |
if __name__ == '__main__': | |
for x in Myfib(1000): | |
print x , | |
print '' | |
iterVar = iter(Myfib(100)) | |
for x in iterVar: | |
print x , |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment