一般认为迭代器就是实现了两个方法__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协程初步---一个迭代器的实现的更多相关文章

  1. python协程初步---一个生成器的实现

    和列表那种一下占据长度为n的内存空间不同的是,生成器在调用的过程中逐步占据内存空间,因此有着很大的优势 一个斐波纳契数列的例子 def myfibbo(num): a,b=, count= while ...

  2. python协程初步--gevent库使用以及解释什么是猴子补丁monkey_patch

    协程工作的特点是遇到阻塞或耗时的任务时就切换,协程的生存依赖于线程,线程依赖于进程 一个似乎有点问题的例子 import gevent,time def kisscpc(num): for i in ...

  3. 关于python协程的一个例子的学习

    例子来自https://blog.tonyseek.com/post/event-manage-with-greenlet/ 加了一些注释看懂了: 注释中的数字表示执行的顺序,这个简单的例子用到了py ...

  4. Python 协程总结

    Python 协程总结 理解 协程,又称为微线程,看上去像是子程序,但是它和子程序又不太一样,它在执行的过程中,可以在中断当前的子程序后去执行别的子程序,再返回来执行之前的子程序,但是它的相关信息还是 ...

  5. 从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 ...

  6. [转载] Python协程从零开始到放弃

    Python协程从零开始到放弃 Web安全 作者:美丽联合安全MLSRC   2017-10-09  3,973   Author: lightless@Meili-inc Date: 2017100 ...

  7. Python协程详解(二)

    上一章,我们介绍了Python的协程,并讲到用yield达到协程的效果,这一章,我们来介绍yield from的结构和作用 我们先来对比下yield和yield from的用法 def first_g ...

  8. python协程详解

    目录 python协程详解 一.什么是协程 二.了解协程的过程 1.yield工作原理 2.预激协程的装饰器 3.终止协程和异常处理 4.让协程返回值 5.yield from的使用 6.yield ...

  9. Python协程与Go协程的区别二

    写在前面 世界是复杂的,每一种思想都是为了解决某些现实问题而简化成的模型,想解决就得先面对,面对就需要选择角度,角度决定了模型的质量, 喜欢此UP主汤质看本质的哲学科普,其中简洁又不失细节的介绍了人类 ...

随机推荐

  1. [转帖]PostgreSQL 昨天,今天和明天

    PostgreSQL 昨天,今天和明天 http://www.postgres.cn/v2/news/viewone/1/52 原作者:何伟平(laser) 创作时间:2005-01-15 11:44 ...

  2. 深入理解 Css3 的 clip-path

    clip-path CSS 属性可以创建一个只有元素的部分区域可以显示的剪切区域.区域内的部分显示,区域外的隐藏.clip-path属性代替了现在已经弃用的剪切 clip属性.clip-path的属性 ...

  3. K8S 从入门到放弃系列文章目录(Kubernetes 1.14)

    1)软件环境 软件 版本 系统 Centos7.5 Kubernetes 1.14.1 Docker 18.09 Calico 3.6 Etcd 3.3.12 2)部署过程简单概要 三台master节 ...

  4. python学习-14 基本数据类型3

    1.字符串 获取字符串的字符,例如: test = 'abcd' a= test[0] # 通过索引,下标,获取字符串中的某一个字符 print(a) b = test[0:1] # 通过下标的 范围 ...

  5. python中检测某个变量是否有定义

    目录 第一种方法使用内置函数locals() 第二种方法使用内置函数dir() 第三种方法使用内置函数vars() 第一种方法使用内置函数locals() 'testvar' in locals(). ...

  6. Hibernate的入门Curd用法

    今天分享的是hibernate关系映射框架的入门用法 一:Hibernate简介 Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建 ...

  7. asp.net core-15.Individual authentication 模板

    在visual studio code执行dotnet new mvc --help 可以查看执行命令 visual studio code先创建一个mvc的项目 dotnet new mvc -au ...

  8. leetcode --165--php

    class Solution { /** * @param String $version1 * @param String $version2 * @return Integer */ functi ...

  9. C#的@标志的使用情况—本篇blog采用Markdown编写

    @(C# 参考--出自官方文档入口) 1.使 C# 关键字用作标识符. @ 字符可作为代码元素的前缀,编译器将把此代码元素解释为标识符而非 C# 关键字. 下面的示例使用 @ 字符定义其在 for 循 ...

  10. mimikatz记录

    mimikatz需要管理员权限运行 vps监听 nc -lvp 4444 服务器管理员权限执行 mimikatz.exe ""privilege::debug"" ...