Created
May 23, 2017 09:20
-
-
Save ericzhong/b3f19e367e64bf36adabd803b9ccfff7 to your computer and use it in GitHub Desktop.
迭代器
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
from collections import Iterator | |
class Foo(Iterator): | |
def __init__(self, stop): | |
self.x = 0 | |
self.stop = stop | |
def __iter__(self): | |
return self | |
def next(self): | |
if self.x < self.stop: | |
i = self.x | |
self.x += 1 | |
return i | |
else: | |
raise StopIteration | |
__next__ = next # Python 3 compatibility | |
foo = Foo(10) | |
for n in foo: | |
print(n), |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment