关于StopIteration的处理


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__())


猜你喜欢

转载自blog.csdn.net/hsc_1/article/details/80702669