def coroutine(func): def inner(*args,**kwargs): f=func(*args,**kwargs) next(f) return f return inner
@coroutine def grep(pattern): print("Looking for %s"%pattern) # try: while True: line =yield if pattern in line: print(line) break return '--done--'
g=grep('hudahai') g.send('hutiankong') try: g.send("hudahai is a handsome boy") except StopIteration as s: print(s.__repr__())