Created
March 9, 2012 17:24
-
-
Save vbmendes/2007646 to your computer and use it in GitHub Desktop.
Exemplo de uso de yield e __iter__
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
def fib(quantidade): | |
a, b = 0, 1 | |
for i in xrange(quantidade): | |
yield b | |
a, b = b, a + b | |
class Fib(object): | |
def __init__(self, quantidade): | |
self.quantidade = quantidade | |
def __iter__(self): | |
return fib(self.quantidade) | |
for i in Fib(10): | |
print i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment