Skip to content

Instantly share code, notes, and snippets.

@marciojrtorres
Created August 9, 2013 01:22
Show Gist options
  • Save marciojrtorres/6190408 to your computer and use it in GitHub Desktop.
Save marciojrtorres/6190408 to your computer and use it in GitHub Desktop.
Geradores (Generators) em Python
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