Skip to content

Instantly share code, notes, and snippets.

@sujayy1983
Created January 25, 2015 02:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sujayy1983/e5f8a927b413b16bec03 to your computer and use it in GitHub Desktop.
Save sujayy1983/e5f8a927b413b16bec03 to your computer and use it in GitHub Desktop.
1. My iterator implementation. 2. Using the iterator in two ways.
"""
@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