python协程初步---一个迭代器的实现
一般认为迭代器就是实现了两个方法__iter__和__next__
- 先创建这样一个类
from collections import Iterable
from collections import Iterator class classiterable(object):
def __iter__(self):
pass
def __next__(self):
pass class mycoach(object):
def __init__(self):
pass
def addname(self):
pass
def __iter__(self):
return classiterable()
cpc = mycoach() print("类对象可以被迭代" if isinstance(cpc,Iterable) else "无法被迭代")
输出结果:
类对象可以被迭代
通过在mycoach类的方法__iter__中返回classiterable实现了mycoach和classiterable类之间的联系
- 实现classiterator访问mycoach类中的属性
from collections import Iterable
from collections import Iterator class classiterable(object):
def __init__(self,obj):
self.obj = obj
self.count = #添加一个计数器,确保按顺序遍历数组 def __iter__(self):
pass
def __next__(self):
#防止迭代过头
if self.count<len(self.obj.coachname):
ret = self.obj.coachname[self.count]
self.count+=
return ret
else:
raise StopIteration class mycoach(object):
def __init__(self):
self.coachname=[]
def addname(self,name):
self.coachname.append(name)
def __iter__(self):
return classiterable(self)
cpc = mycoach()
cpc.addname('陈培昌')
cpc.addname('程劲')
cpc.addname('徐晓冬')
for i in cpc:
print(i)
print("类对象可以被迭代" if isinstance(cpc,Iterable) else "无法被迭代")
输出结果:
陈培昌
程劲
徐晓冬
类对象可以被迭代
- 完全进化版本---mycoach内部实现__next__魔术方法
class mycoach(object):
def __init__(self):
self.coachname=[]
self.count=
def addname(self,name):
self.coachname.append(name)
def __iter__(self):
return self
def __next__(self):
if self.count<len(self.coachname):
ret = self.coachname[self.count]
self.count+=
return ret
else:
raise StopIteration cpc = mycoach()
cpc.addname('陈培昌')
cpc.addname('程劲')
cpc.addname('徐晓冬')
for i in cpc:
print(i)
print("类对象可以被迭代" if isinstance(cpc,Iterable) else "无法被迭代")
输出结果:
陈培昌
程劲
徐晓冬
类对象可以被迭代
- 斐伯纳契数列
class mywenwa(object):
def __init__(self,num):
self.a,self.b=,
self.count=
self.times=num
def __iter__(self):
return self
def __next__(self):
if self.count <self.times:
self.a,self.b = self.a+self.b,self.a
ret = self.b
return ret
else:
raise StopIteration
saiwa = mywenwa()
print(next(saiwa))
print(next(saiwa))
print(next(saiwa))
print(next(saiwa))
print(next(saiwa))
print(next(saiwa))
print(next(saiwa))
print(next(saiwa))
print(next(saiwa))
.......
输出结果:
python协程初步---一个迭代器的实现的更多相关文章
- python协程初步---一个生成器的实现
和列表那种一下占据长度为n的内存空间不同的是,生成器在调用的过程中逐步占据内存空间,因此有着很大的优势 一个斐波纳契数列的例子 def myfibbo(num): a,b=, count= while ...
- python协程初步--gevent库使用以及解释什么是猴子补丁monkey_patch
协程工作的特点是遇到阻塞或耗时的任务时就切换,协程的生存依赖于线程,线程依赖于进程 一个似乎有点问题的例子 import gevent,time def kisscpc(num): for i in ...
- 关于python协程的一个例子的学习
例子来自https://blog.tonyseek.com/post/event-manage-with-greenlet/ 加了一些注释看懂了: 注释中的数字表示执行的顺序,这个简单的例子用到了py ...
- Python 协程总结
Python 协程总结 理解 协程,又称为微线程,看上去像是子程序,但是它和子程序又不太一样,它在执行的过程中,可以在中断当前的子程序后去执行别的子程序,再返回来执行之前的子程序,但是它的相关信息还是 ...
- 从yield 到yield from再到python协程
yield 关键字 def fib(): a, b = 0, 1 while 1: yield b a, b = b, a+b yield 是在:PEP 255 -- Simple Generator ...
- [转载] Python协程从零开始到放弃
Python协程从零开始到放弃 Web安全 作者:美丽联合安全MLSRC 2017-10-09 3,973 Author: lightless@Meili-inc Date: 2017100 ...
- Python协程详解(二)
上一章,我们介绍了Python的协程,并讲到用yield达到协程的效果,这一章,我们来介绍yield from的结构和作用 我们先来对比下yield和yield from的用法 def first_g ...
- python协程详解
目录 python协程详解 一.什么是协程 二.了解协程的过程 1.yield工作原理 2.预激协程的装饰器 3.终止协程和异常处理 4.让协程返回值 5.yield from的使用 6.yield ...
- Python协程与Go协程的区别二
写在前面 世界是复杂的,每一种思想都是为了解决某些现实问题而简化成的模型,想解决就得先面对,面对就需要选择角度,角度决定了模型的质量, 喜欢此UP主汤质看本质的哲学科普,其中简洁又不失细节的介绍了人类 ...
随机推荐
- [转帖]Greenplum :基于 PostgreSQL 的分布式数据库内核揭秘 (上篇)
Greenplum :基于 PostgreSQL 的分布式数据库内核揭秘 (上篇) https://www.infoq.cn/article/3IJ7L8HVR2MXhqaqI2RA 学长的文章.. ...
- Java搭建环境和工具安装详细教程
.一.搭建java运行环境 总体分为两个步骤 1.下载JDK( java dovelop kit 简称 java 开发工具) 首先我们直接下载java开发工具包JDK,转到Oracle官网 下载链接 ...
- Druid基本配置
最近公司要用Druid 所以看了下基本配置及配置过程中出现的问题 Druid是什么? Druid是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0.DBCP.PROXOOL等DB池的优点,同时 ...
- MySQL 数据库连接命令
启动数据库服务 格式: net start 数据库名 net start mysql57 关闭数据库服务 格式: net stop 数据库名 net stop mysql57 链接 ...
- go hello world第一个程序
main 函数所在的包名必须使用main import "fmt" 导入包fmt fmt包包含了Println方法的定义 func main() 程序运行入口方法和c语言相似 ...
- 【动态规划】Überwatch
Überwatch 题目描述 The lectures are over, the assignments complete and even those pesky teaching assista ...
- IDEA如何本机调试springboot应用打的jar包
背景: 我用命名行 执行 java -jar ***.jar发现 springboot启动时抛出错误,因此想debug进去看看究竟为什么出错. 1 在命令行执行 java -jar -Xdebug ...
- 谷歌(google)广告尺寸大小列表
在平时做网页模板时,我们需要计算Google AdSense 的尺寸,以确保它能和页面完美的结合,提高AdSense的点击率,进台后看又很麻烦,下面整理了Google 广告的各种尺寸,跟大家分享一下. ...
- sql 添加变量
在sql语句中添加变量. declare @local_variable data_type 声明时需要指定变量的类型, 可以使用set和select对变量进行赋值, 在sql语句中就可以使用@loc ...
- vue 实现的评分小星星组件,包括半星
github源码地址:https://github.com/13476075014/node-vue/blob/master/mynodeproject/13.sell/sell/src/compon ...