如果要达到多个迭代器的效果,__iter__()只需替迭代器定义新的状态对象,而不是返回self

 class SkipIterator:
def __init__(self, wrapped):
self.wrapped = wrapped
self.offset = 0 def __next__(self):
if self.offset >= len(self.wrapped):
raise StopIteration
else:
item = self.wrapped[self.offset]
self.offset+=2
return item class SkipObject:
def __init__(self, wrapped):
self.wrapped = wrapped def __iter__(self):
return SkipIterator(self.wrapped) if __name__ == '__main__':
hhh = 'hahaha'
skipper = SkipObject(hhh)
for i in skipper:
print(i) I = iter(skipper)
print(next(I), next(I), next(I))

参考资料:Python学习手册

最新文章

  1. TCP/IP 七层协议
  2. 利用cubieboard设置samba打印服务器
  3. STM32 DAC的配置与使用
  4. redis的主从复制部署和使用
  5. C# Windows Forms 事件处理顺序
  6. linux-阿里云ECS部署PPTP(centos)
  7. LeetCode Balanced Binary Tree (判断平衡树)
  8. 博客终于开通了happy
  9. anroid里面的post请求
  10. debounce去弹跳
  11. C# Value type vs Reference type
  12. mysql(4)—— 表连接查询与where后使用子查询的性能分析。
  13. 利用 xrandr 命令修改屏幕分辨率
  14. [APIO2008]免费道路(生成树)
  15. 170814、Java使用gzip压缩文件、还原文件
  16. RW RO ZI ROM keil中的含义
  17. "Error: ANDROID_HOME is not set and "android" command not in your PATH. You must fulfill at least one of these conditions.".
  18. 【react读取文件】react发送GET请求读取静态文件
  19. git推送到github报错:error: The requested URL returned error: 403 Forbidden while accessing https://github.com
  20. Zabbix 3.4.11版本 自定义监控项

热门文章

  1. Stub和Mock的理解
  2. nc 简单的使用
  3. Android 手机进入不了fastboot模式的解决方案
  4. COFF/PE文件结构
  5. API接口服务端
  6. 编程利用利用curses库编程开始
  7. Linux 内核源码中likely()和unlikely()
  8. spring mvc后台接收中文乱码
  9. C语言之形参和实参
  10. oracle 增加表空间