Created
August 9, 2013 01:22
-
-
Save marciojrtorres/6190408 to your computer and use it in GitHub Desktop.
Geradores (Generators) em Python
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
def impares(max = 2^16-1): | |
for n in range(max): | |
if n % 2 == 1: | |
print 'gerando ...' | |
yield n | |
# situação de uso: | |
for n in impares(10): | |
print 'percorrendo ...' | |
print n | |
# saída: | |
# | |
# gerando ... | |
# percorrendo ... | |
# 1 | |
# gerando ... | |
# percorrendo ... | |
# 3 | |
# gerando ... | |
# percorrendo ... | |
# 5 | |
# gerando ... | |
# percorrendo ... | |
# 7 | |
# gerando ... | |
# percorrendo ... | |
# 9 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment