您的位置:首页 > 编程语言 > Python开发

协程简单程序(python3.5)

2016-07-29 15:05 218 查看
pycharm中编写的plx12:
def
print_matches(matchtxt):

print("looking for ",matchtxt)

while True:

line=(yield)

if matchtxt
in line :

print(line)
命令窗口运行:
>>> import sys
>>> sys.path.append("D:\plx")
>>> import plx12
>>> matcher=plx12.print_matches("python")
>>> matcher.__next__()
lookingfor
python
>>> matcher.send("hello word")
>>> matcher.send("python is cool")
pythonis cool
>>> matcher.send("yow")
>>>matcher.close()
该程序实现的是简单的匹配。
利用(yield)与send()配合使用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: