Created
October 2, 2012 22:19
-
-
Save wrunk/3823685 to your computer and use it in GitHub Desktop.
Simple python iterator class
This file contains hidden or 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
class TestIter(object): | |
def __init__(self): | |
self.i = 0 | |
self.l = [1,2,3,4] | |
def __iter__(self): | |
return self | |
def next(self): | |
if self.i + 1 <= len(self.l): | |
result = self.l[self.i] | |
self.i += 1 | |
return result | |
raise StopIteration | |
t = TestIter() | |
for i in t: | |
print i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment