Skip to content

Instantly share code, notes, and snippets.

@christophevg
Created November 26, 2014 11:19
Show Gist options
  • Save christophevg/61865cf042a68727a552 to your computer and use it in GitHub Desktop.
Save christophevg/61865cf042a68727a552 to your computer and use it in GitHub Desktop.
Illustration of Python generators
# Illustration of Python generators
def count_down(size):
print "starting generation"
while size > 1:
print "yielding"
yield size
size -= 1
print "finishing generation"
yield size
if __name__ == "__main__":
print "start"
gen = count_down(3)
print "next = " + str(next(gen))
print "next = " + str(next(gen))
print "next = " + str(next(gen))
print "stop"
# start
# starting generation
# yielding
# next = 3
# yielding
# next = 2
# finishing generation
# next = 1
# stop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment