【Python】在Python中自定义迭代器Iterator
来源:互联网 发布:网络诈骗教育考试题库 编辑:程序博客网 时间:2024/05/16 17:19
Python中迭代器本质上是每次调用.next()都返回一个元素或抛出StopIteration的容器对象。
在Python中其实没有“迭代器”这个类,具有以下2个特性的类都可以被称为“迭代器”类:
1、有next方法,返回容器的下一个元素或抛出StopIteration异常;
2、有__iter__方法,返回迭代器本身;
自定义迭代器的例子(来自《Expert Python Programming(Python高级编程)》)
python2.x版本:
class MyIterator(object):def __init__(self, step):self.step = stepdef next(self):"""Returns the next element."""if self.step == 0:raise StopIteratorself.step -= 1return self.stepdef __iter__(self):"""Returns the iterator itself."""return self
- 【Python】在Python中自定义迭代器Iterator
- python中使用迭代器 Iterator
- Python 中iterator
- [Python]在Python中添加自定义模块
- Python 迭代器(iterator)
- Python迭代器(Iterator)
- python的迭代器iterator
- python中的迭代器Iterator
- python: 迭代器 (Iterator)
- Python iterator迭代器
- Python中iterator的理解
- Python中Iterable与Iterator
- 在Python中添加自定义模块
- 在Python中添加自定义模块
- 在IDL中调用自定义Python Module
- 在Python中添加自定义模块
- 在Python中添加自定义模块
- Learning Python : 3. iterator 迭代器
- 初始化:变量和数组
- unity4.x_error(一)
- Zigbee 获取设备地址信息
- zigbee 协议栈数据类型及转换
- wp-启动器与选择器
- 【Python】在Python中自定义迭代器Iterator
- zigbee zcl规范及其协议栈实现1
- GetWindowLong与SetWindowLong
- Android学习之创建和使用数据库
- zigbee zcl规范及其协议栈实现2
- 我那之大手机导航刷卡机
- Two Methods of Finding the sum of 1! + 2! +3! + 4! +...+ 20!
- zigbee zcl规范及其协议栈实现3 读取服务器端属性值
- 【leetcode】Remove Duplicates from Sorted List II