Created
January 26, 2015 14:18
-
-
Save justino/b95eb0fe338377ee4d46 to your computer and use it in GitHub Desktop.
Python Generators Example
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
#!/usr/bin/python | |
import time | |
def f(): | |
print "f(): Function 'f' has been called" | |
print "f(): I can do a bunch of junk here" | |
print "f(): Maybe start reading in a file line by line" | |
print "f(): Going to sleep for 2 seconds\n" | |
time.sleep(2) | |
print "f(): About to yield for first time" | |
yield 1 | |
print "f(): Have yielded once already, lets do it again" | |
yield 2 | |
print "f(): Have yielded twice now, one more time" | |
yield 3 | |
print "f(): Have yielded three times now, and that was the last time, not going to return any more data, going to sleep for 5 seconds" | |
time.sleep(5) | |
print "main: Program Started" | |
print "main: About to start loop" | |
for val in f(): | |
print "main: Got value: %i, lets pause for a couple of seconds before continuing the loop\n" % (val) | |
time.sleep(2) | |
print "main: Finished looping, lets end the program" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment