Site Tools


python:coroutines

Python Coroutines

# Sent values are returned by yield
def grep(pattern):
    print "Looking for %s" % pattern
    while True:
        line = (yield)
        if pattern in line:
            print(line)
 
g = grep("python")
g.next()
g.send("python generators rock!")
python/coroutines.txt · Last modified: 2020-04-19 (external edit)