Created
February 17, 2011 12:59
-
-
Save wjzhangq/831682 to your computer and use it in GitHub Desktop.
python yield 用法
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
class Fib: | |
def __init__(self, max): | |
self.max = max | |
def __iter__(self): | |
self.a = 0 | |
self.b = 1 | |
return self | |
def next(self): | |
fib = self.a | |
if fib > self.max: | |
raise StopIteration | |
self.a, self.b = self.b, self.a + self.b | |
return fib | |
for n in Fib(100): | |
print n |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment