__iter__
【__iter__】
如果一个类想被用于for ... in
循环,类似list或tuple那样,就必须实现一个__iter__()
方法,该方法返回一个迭代对象,然后,Python的for循环就会不断调用该迭代对象的next()
方法拿到循环的下一个值,直到遇到StopIteration错误时退出循环。
我们以斐波那契数列为例,写一个Fib类,可以作用于for循环:
现在,试试把Fib实例作用于for循环:
container.
__iter__
()
Return an iterator object. The object is required to support the iterator protocol described below.
The iterator objects themselves are required to support the following two methods, which together form the iterator protocol:
iterator.
__iter__
()-
Return the iterator object itself. This is required to allow both containers and iterators to be used with the
for
andin
statements.
iterator.
__next__
()-
Return the next item from the container. If there are no further items, raise the
StopIteration
exception.
参考:https://docs.python.org/3.5/library/stdtypes.html#typeiter
__iter__的更多相关文章
- python 使用__neg__和__iter__
__neg__ python中 __neg__ 方法对应于 符号 - 可见 str 没有__neg__,定义 strnew 好吧,无法再简化了 __iter__ 看看 list 的 __iter__: ...
- Python 迭代器 & __iter__方法
转载来自: http://blog.csdn.net/bluebird_237/article/details/38894617 迭代器就是重复地做一些事情,可以简单的理解为循环,在python中实现 ...
- 一个网友写的栈,问为啥不能迭代。具有__iter__ 和next方法的对象叫迭代器-七七巴巴黄页网
一个网友写的栈,问为啥不能迭代.具有__iter__ 和next方法的对象叫迭代器-七七巴巴黄页网 一个网友写的栈,问为啥不能迭代.具有__iter__ 和next方法的对象叫迭代器 python视频 ...
- 使用__slots__ __str__ __iter__
__slots__ 为了达到限制的目的,Python允许在定义class的时候,定义一个特殊的__slots__变量,来限制该class实例能添加的属性. __str__ 用这个命令定义方法,可以返 ...
- __iter__ 和 __next__
class F: def __init__(self,x): self.x = x def __iter__(self): #把对象 变成可迭代对象 return self def __next__( ...
- __slots__,__doc__,__del__,__call__,__iter__,__next__迭代器协议(三十六)
1.__slots__是什么:是一个类变量,变量值可以是列表,元祖,或者可迭代对象,也可以是一个字符串(意味着所有实例只有一个数据属性) 2.引子:使用点来访问属性本质就是在访问类或者对象的__dic ...
- python魔法函数(二)之__getitem__、__len__、__iter__
魔法函数会增强python类的类型,独立存在 __getitem class Company: def __init__(self, employees): self.employees = empl ...
- python中的__iter__ __reversed__ __next__
__reversed__ 返回集合的倒叙迭代器,如果没有实现这个方法,reversed()会去使用__getitem__和__len__来实现 介绍__next__和 __iter__方法需要了解下可 ...
- __iter__的有无
迭代器和生成器 1.迭代器 我们之前⼀直在⽤可迭代对象进⾏迭代操作. 那么到底什么是可迭代对象.⾸先我们先回顾⼀下⽬前我们所熟知的可迭代对象有哪些: str, list, tuple, dict, s ...
随机推荐
- jQuery-2.1.4.min.js:4 Uncaught TypeError: Illegal invocation
jQuery-2.1.4.min.js:4 Uncaught TypeError: Illegal invocation 此错误与crsf有关
- git使用流程
1,配置sshkey: ssh-keygen -t rsa -C "Github 的注册邮箱" //创建本地 ssh 在 Github 中添加这个 sshkey : 复制 id_ ...
- day17算法
http://www.360doc.com/content/14/0804/11/1073512_399302715.shtml
- TTS多音字问题
今天在CSDN上找到了解决方案,终于解决了多音字问题. Text1.Text = "<pron sym='jia 3'> 贾</pron>宝玉,商<pron ...
- sp_change_users_login解决孤立用户问题
孤立帐户,指的是某个数据库的帐户只有用户名而没有登录名,这样的用户在用户库的sysusers系统表中存在,而在master数据库的syslogins中却没有对应的记录. 孤立帐户的产生一般是一下两种: ...
- Sedgewick的红黑树
红黑树一直是数据结构中的难点,大部分关于算法与数据结构的学习资料(包括<算法导论>)对于这部分的讲解都是上来就下定义,告诉我们红黑树这个性质那个性质,插入删除要注意1234点,但是基本没有 ...
- Window环境下 Git 下载Android源码
1.需要的工具 git.vpn代理 2. 设置git代理(Google source 无法下载,git设置代理) git config --global http.proxy "localh ...
- ECharts使用心得总结(二)
Echarts使用心得总结(二) 前言: 前一段时间一直都挺忙的,各种事,也没来得及每周的总结,趁着晚上的一点时间把项目中用的Echart中常用的各种图表给抽象总结了一下,趁着周末跟大家分享一下.之前 ...
- c#操作文件夹得读写权限
对文件夹设置为Everyone的权限,首先需要先添加引用 using System.Security.AccessControl; 采用下面的方法对文件夹设置Everyone权限 /// < ...
- 使用-MM生成include指令和依赖生成(make include directive and dependency generation with -MM)
I want a build rule to be triggered by an include directive if the target of the include is out of d ...