设计模式-迭代器模式-iterator-python
2017-08-10 01:09
387 查看
def
它提供一种方法访问一个容器对象中各个元素, 而又不需暴露该对象的内部细节。usage
迭代器模式提供了遍历容器的方便性, 容器只要管理增减元素就可以了, 需要遍历时交由迭代器进行。code
from __future__ import print_function def count_to(count): """Counts by word numbers, up to a maximum of five""" numbers = ["one", "two", "three", "four", "five"] # enumerate() returns a tuple containing a count (from start which # defaults to 0) and the values obtained from iterating over sequence for pos, number in zip(range(count), numbers): yield number # Test the generator count_to_two = lambda: count_to(2) count_to_five = lambda: count_to(5) print('Counting to two...') for number in count_to_two(): print(number, end=' ') print() print('Counting to five...') for number in count_to_five(): print(number, end=' ') print() ### OUTPUT ### # Counting to two... # one two # Counting to five... # one two three four five
相关文章推荐
- [C++设计模式] iterator 迭代器模式
- 乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern)
- 设计模式:迭代器模式(Iterator)
- 我所理解的设计模式(C++实现)——迭代器模式(Iterator Pattern)
- 设计模式迭代器模式(IteratorPattern)
- 设计模式 ( 十四 ) 迭代器模式Iterator(对象行为型)
- Java设计模式——迭代器模式(Iterator Pattern)
- 设计模式 - 迭代器模式(iterator pattern) 具体解释
- Python使用设计模式中的责任链模式与迭代器模式的示例
- 设计模式 ( 十四 ) 迭代器模式Iterator(对象行为型)
- 设计模式:迭代器模式(Iterator)
- 设计模式:迭代器模式(Iterator)
- 设计模式 - 迭代器模式(iterator pattern) 详解
- 设计模式 - Iterator 模式(迭代器模式)
- 设计模式 ( 十四 ) 迭代器模式Iterator(对象行为型)
- Python使用设计模式中的责任链模式与迭代器模式的示例
- 设计模式之三:迭代器模式(IteratorPattern)
- 设计模式C++学习笔记之十四(Iterator迭代器模式)
- .NET设计模式(18):迭代器模式(Iterator Pattern)
- 设计模式C++学习笔记之十四(Iterator迭代器模式)