This shows you the differences between two versions of the page.
| python:coroutines [2015-11-07] – created dcai | python:coroutines [2020-04-19] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Python Coroutines ====== | ||
| + | <code python> | ||
| + | # Sent values are returned by yield | ||
| + | def grep(pattern): | ||
| + | print " | ||
| + | while True: | ||
| + | line = (yield) | ||
| + | if pattern in line: | ||
| + | print(line) | ||
| + | | ||
| + | g = grep(" | ||
| + | g.next() | ||
| + | g.send(" | ||
| + | </ | ||