楼主: tzj110
6282 29

[书籍介绍] Python Cookbook, 3rd Edition [推广有奖]

21
Nicolle 学生认证  发表于 2016-5-6 08:57:51
提示: 作者被禁止或删除 内容自动屏蔽

22
Nicolle 学生认证  发表于 2016-5-6 09:03:33
提示: 作者被禁止或删除 内容自动屏蔽

23
sacromento 学生认证  发表于 2016-7-24 04:33:19
谢谢分享啊!

24
phipe 发表于 2016-8-27 20:57:34
谢谢分享

25
ReneeBK 发表于 2016-12-29 10:00:55
  1. 4.1. Manually Consuming an Iterator
  2. Problem
  3. You need to process items in an iterable, but for whatever reason, you can’t or don’t want
  4. to use a for loop.
  5. Solution
  6. To manually consume an iterable, use the next() function and write your code to catch
  7. the StopIteration exception. For example, this example manually reads lines from a
  8. file:
  9. with open('/etc/passwd') as f:
  10. try:
  11. while True:
  12. line = next(f)
  13. print(line, end='')
  14. except StopIteration:
  15. pass
复制代码

26
ReneeBK 发表于 2016-12-29 10:04:16
  1. 4.2. Delegating Iteration
  2. Problem
  3. You have built a custom container object that internally holds a list, tuple, or some other
  4. iterable. You would like to make iteration work with your new container.
  5. Solution
  6. Typically, all you need to do is define an __iter__() method that delegates iteration to
  7. the internally held container. For example:
  8. class Node:
  9. def __init__(self, value):
  10. self._value = value
  11. self._children = []
  12. def __repr__(self):
  13. return 'Node({!r})'.format(self._value)
  14. def add_child(self, node):
  15. self._children.append(node)
  16. def __iter__(self):
  17. return iter(self._children)
  18. # Example
  19. if __name__ == '__main__':
  20. root = Node(0)
  21. child1 = Node(1)
  22. child2 = Node(2)
  23. root.add_child(child1)
  24. root.add_child(child2)
  25. for ch in root:
  26. print(ch)
  27. # Outputs Node(1), Node(2)
复制代码

27
eeabcde 发表于 2017-10-10 10:38:48
多谢 多谢

28
ttuc123 发表于 2017-10-24 10:16:37 来自手机
tzj110 发表于 2014-7-22 13:43
非常感谢。楼主辛苦!!

29
baiwei1637124 学生认证  发表于 2017-10-26 19:21:20
楼主威武,多谢分享~

30
芸月樱 发表于 2017-11-18 22:54:39
听说中文版错误多,直接来看英文版了

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-12 03:39