Created
October 19, 2011 02:27
-
-
Save zeeshanlakhani/1297342 to your computer and use it in GitHub Desktop.
Coroutine Simple Example Python
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
# grep.py | |
# | |
# A very simple coroutine | |
def grep(pattern): | |
print "Looking for %s" % pattern | |
while True: | |
line = (yield) | |
if pattern in line: | |
print line, | |
# Example use | |
if __name__ == '__main__': | |
g = grep("python") | |
g.next() | |
g.send("Yeah, but no, but yeah, but no") | |
g.send("A series of tubes") | |
g.send("python generators rock!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment