#!/usr/bin/python
#coding=utf-8
'''
Created on 2017年11月23日 from home
@author: James zhan
'''
def counter(start_at=0):
count=start_at
while True:
val=(yield count)
print type(val)
print val
if val is not None:
count=val
else:
count+=1
count=counter(5)
print count.next()
print count.next()
print count.next()
count.send(9)
print count.next()
执行结果:
5
<type 'NoneType'>
None
6
<type 'NoneType'>
None
7
<type 'int'>
9
<type 'NoneType'>
None
10
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】Flutter适配HarmonyOS 5知识地图,实战解析+高频避坑指南
【推荐】开源 Linux 服务器运维管理面板 1Panel V2 版本正式发布
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步