diff --git a/cookbook/c04/p02_delegate_iter.py b/cookbook/c04/p02_delegate_iter.py new file mode 100644 index 0000000..5111a33 --- /dev/null +++ b/cookbook/c04/p02_delegate_iter.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- +""" +Topic: 代理迭代 +Desc : +""" + + +class Node: + def __init__(self, value): + self._value = value + self._children = [] + + def __repr__(self): + return 'Node({!r})'.format(self._value) + + def add_child(self, node): + self._children.append(node) + + def __iter__(self): + return iter(self._children) + +# Example +if __name__ == '__main__': + root = Node(0) + child1 = Node(1) + child2 = Node(2) + root.add_child(child1) + root.add_child(child2) + # Outputs Node(1), Node(2) + for ch in root: + print(ch) diff --git a/source/c04/p02_delegating_iteration.rst b/source/c04/p02_delegating_iteration.rst index 58ee86b..a7d14de 100644 --- a/source/c04/p02_delegating_iteration.rst +++ b/source/c04/p02_delegating_iteration.rst @@ -5,14 +5,53 @@ ---------- 问题 ---------- -todo... +你构建了一个自定义容器对象,里面包含有列表、元组或其他可迭代对象。 +你想直接在你的这个新容器对象上执行迭代操作。 + +| ---------- 解决方案 ---------- -todo... +实际上你只需要定义一个__iter__()方法,将迭代操作代理到容器内部的对象上去。比如: + +.. code-block:: python + + class Node: + def __init__(self, value): + self._value = value + self._children = [] + + def __repr__(self): + return 'Node({!r})'.format(self._value) + + def add_child(self, node): + self._children.append(node) + + def __iter__(self): + return iter(self._children) + + # Example + if __name__ == '__main__': + root = Node(0) + child1 = Node(1) + child2 = Node(2) + root.add_child(child1) + root.add_child(child2) + # Outputs Node(1), Node(2) + for ch in root: + print(ch) + +在上面代码中,__iter__()方法只是简单的将迭代请求传递给内部的_children属性。 + +| ---------- 讨论 ---------- -todo... \ No newline at end of file +Python的迭代器协议需要__iter__()方法返回一个实现了__next__()方法的迭代器对象。 +如果你只是迭代遍历其他容器的内容,你无须担心底层是怎样实现的。你所要做的只是传递迭代请求既可。 + +这里的iter()函数的使用简化了代码,iter(s)只是简单的通过调用s.__iter__()方法来返回对应的迭代器对象, +就跟len(s)会调用s.__len__()原理是一样的。 +