Python3中简单的迭代器程序
2024-09-07 04:01:54
1.迭代器程序(实现菲比那次数列并且可以抛出与接收异常)
def fib(max):
n,a,b = 0,0,1
while n < max:
#print(b)
yield b
a,b = b,a+b
n= n+1
return 'done'
g = fib(10)
while True:
try:
x = g.__next__()
print('g:',x)
except StopIteration as e:
print('Generator return value:',e.value)
break
2.利用迭代器实现简单的“单线程并行”吃包子(实际上还是串行)
import time
def consumer(name):
print('[%s] prepare to eat baozi!!'%name)
while True:
baozi = yield
print('baozi [%s] is coming,ate by [%s]'%(baozi,name)) def producer(name):
c = consumer('A')
c2 = consumer('B')
c.__next__()
c2.__next__()
print('start'.center(30,'-'))
for i in range(10):
time.sleep(1)
print('just one,half to half')
c.send(i)
c2.send(i) producer('wt')
最新文章
- 在Oracle中使用Entity Framework 6 CodeFirst
- 数据库递归查询-CTE
- IntelliJ IDEA 的 Java 热部署插件 JRebel 安装及使用
- Jordan Lecture Note-3: 梯度投影法
- java模拟http post
- 一个很奇特的异常 tmpFile.renameTo(classFile) failed
- sun.proxy.$Proxy0 cannot be cast to dao.impl.MyAdviceImpl
- Webstrom 常用操作记录
- 错误:&#39;dict&#39; object is not callable
- 查看 Apache并发请求数及其TCP连接状态【转】
- Android OpenGL ES 开发(五): OpenGL ES 使用投影和相机视图
- LabView(控件部分)
- ReactiveCocoa(II)
- win10和ubuntu16.04双系统Geom Error
- vue的采坑之旅--vue-cli脚手架loader重复--Invalid CSS after ";...load the styles";: expected 1 selector or at-rule
- JavaBean(web基础学习笔记十二)
- JAVA问题定位跟踪技术
- 生存分析/Weibull Distribution韦布尔分布
- c/c++中关于String类型的思考
- 数据结构--ADT
热门文章
- 7.Selenium+Python实现搜索百度的测试用例
- Oracle存储过程返回
- 使用内省的方式操作JavaBean
- 2015.9.28 不能将多个项传入“Microsoft.Build.Framework.ITaskItem”类型的参数 问题解决
- composer update的错误使用以及如何更新composer.lock文件
- 关于struts2.3的action
- .net 4 安装未成功,无意中的解决办法!
- spring-初始化bean
- Python操作远程机器
- Python之POST登录测试