Skip to content

Instantly share code, notes, and snippets.

@ericzhong
Created May 23, 2017 09:20
Show Gist options
  • Save ericzhong/b3f19e367e64bf36adabd803b9ccfff7 to your computer and use it in GitHub Desktop.
Save ericzhong/b3f19e367e64bf36adabd803b9ccfff7 to your computer and use it in GitHub Desktop.
迭代器
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