__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 and instatements.

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__的更多相关文章

  1. python 使用__neg__和__iter__

    __neg__ python中 __neg__ 方法对应于 符号 - 可见 str 没有__neg__,定义 strnew 好吧,无法再简化了 __iter__ 看看 list 的 __iter__: ...

  2. Python 迭代器 & __iter__方法

    转载来自: http://blog.csdn.net/bluebird_237/article/details/38894617 迭代器就是重复地做一些事情,可以简单的理解为循环,在python中实现 ...

  3. 一个网友写的栈,问为啥不能迭代。具有__iter__ 和next方法的对象叫迭代器-七七巴巴黄页网

    一个网友写的栈,问为啥不能迭代.具有__iter__ 和next方法的对象叫迭代器-七七巴巴黄页网 一个网友写的栈,问为啥不能迭代.具有__iter__ 和next方法的对象叫迭代器 python视频 ...

  4. 使用__slots__ __str__ __iter__

    __slots__ 为了达到限制的目的,Python允许在定义class的时候,定义一个特殊的__slots__变量,来限制该class实例能添加的属性. __str__  用这个命令定义方法,可以返 ...

  5. __iter__ 和 __next__

    class F: def __init__(self,x): self.x = x def __iter__(self): #把对象 变成可迭代对象 return self def __next__( ...

  6. __slots__,__doc__,__del__,__call__,__iter__,__next__迭代器协议(三十六)

    1.__slots__是什么:是一个类变量,变量值可以是列表,元祖,或者可迭代对象,也可以是一个字符串(意味着所有实例只有一个数据属性) 2.引子:使用点来访问属性本质就是在访问类或者对象的__dic ...

  7. python魔法函数(二)之__getitem__、__len__、__iter__

    魔法函数会增强python类的类型,独立存在 __getitem class Company: def __init__(self, employees): self.employees = empl ...

  8. python中的__iter__ __reversed__ __next__

    __reversed__ 返回集合的倒叙迭代器,如果没有实现这个方法,reversed()会去使用__getitem__和__len__来实现 介绍__next__和 __iter__方法需要了解下可 ...

  9. __iter__的有无

    迭代器和生成器 1.迭代器 我们之前⼀直在⽤可迭代对象进⾏迭代操作. 那么到底什么是可迭代对象.⾸先我们先回顾⼀下⽬前我们所熟知的可迭代对象有哪些: str, list, tuple, dict, s ...

随机推荐

  1. uva674 Coin Change ——完全背包

    link:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  2. 第七课第三节,T语言流程语句(版本5.0)

    for语句 for和while语句一样,都是用来做循环操作的,只是他们的使用方法不一样 (注:关键字,for,end) 执行流程图解: 实例代码: for(var i=0;i<20;i++) / ...

  3. 在yii中使用多个数据库

    背景: 对于一个大公司拥有多个分公司的应用场景下,我们通常需要配置多个sub-database(子数据库)来存储不同的数据纪录. 配置步骤: 1.在application骨架里面的主配置文件main. ...

  4. python thread 多线程

    thread 模块在python3中改为_thread,网上的各种帖子都说不建议新手使用thread,好吃不好吃总得尝尝看. import _thread def print_num(): for i ...

  5. 非常好的分页组建layPage和 layer层特效

    http://layer.layui.com/ http://sentsin.com/layui/laypage/

  6. vs2010编译出的exe“应用程序无法正常启动(0xc0150002)”

    今天编译出一个使用ogre1.6.5动态库的应用程序,启动时报"应用程序无法正常启动(0xc0150002)"的错误提示. 编译环境是Win10+VS2010.这个错误可以在Win ...

  7. struts2 的验证框架validation如何返回json数据 以方便ajax交互

    struts2 的验证框架validation简单,好用,但是input只能输出到jsp页面通过struts2的标签<s:fielderror  />才能取出,(EL应该也可以). 如果使 ...

  8. unity, 播放循环背景音乐注意事项

    循环背景音乐用wav格式,不要用mp3. 参考:http://answers.unity3d.com/questions/343057/how-do-i-make-unity-seamlessly-l ...

  9. 使用yield关键字来提高性能

    比如我们在开发当中往往会遇到这样的问题: public List<string> FindBobs(string [] names)   { List<string> bobs ...

  10. [solr] - suggestion

    前文使用了SpellCheck做了个自动完成模拟(Solr SpellCheck),使用第一种SpellCheck方式做auto-complete,是基于动态代码方式建立内容,下面方式可通过读文件方式 ...