美文网首页
Python迭代器模式

Python迭代器模式

作者: 虾想家 | 来源:发表于2017-03-19 16:09 被阅读39次

迭代器模式,不一次性直接返回结果,调用一次方法,返回一个。

class Iterator(object):
    def __init__(self, iterator_list):
        super().__init__()
        self.list = iterator_list
        self.current_index = 0
        self.max_index = 0

    def __iter__(self):
        self.current_index = 0
        self.max_index = len(self.list) - 1
        return self

    def __next__(self):
        if self.current_index <= self.max_index:
            one = self.list[self.current_index]
            self.current_index += 1
            return one
        else:
            raise StopIteration


def main():
    iterator = Iterator([5, 9, 7, 'x', 'e'])
    for one in iterator:
        print(one)


if __name__ == '__main__':
    main()

相关文章

  • Python 中的迭代器

    Python 中的迭代器 Python 3中的迭代器 容器与迭代器 在Python 3中,支持迭代器的容器,只要支...

  • 设计模式(17) 迭代器模式

    迭代器模式 基于IEnumerable的实现 使用场景 迭代器模式的优缺点 迭代器模式 迭代器模式用于顺序访问集合...

  • Python迭代器模式

    迭代器模式,不一次性直接返回结果,调用一次方法,返回一个。

  • 第5章 -行为型模式-迭代器模式

    一、迭代器模式的简介 二、迭代器模式的优缺点 三、迭代器模式的实例

  • 行为型模式:迭代器模式

    LieBrother原文:行为型模式:迭代器模式 十一大行为型模式之六:迭代器模式。 简介 姓名 :迭代器模式 英...

  • Iterator模式

    迭代器模式(Iterator模式) 定义: 迭代器(Iterator)模式,又叫做游标(Cursor)模式。 ...

  • 迭代器模式

    一、迭代器模式介绍 二、迭代器模式代码实例

  • 理解迭代器/生成器

    完全理解Python 迭代对象、迭代器、生成器 迭代和生成操作总结 Python迭代器 http://www.ji...

  • 设计模式 | 迭代器模式及典型应用

    本文的主要内容: 介绍迭代器模式 源码分析迭代器模式的典型应用Java集合中的迭代器模式Mybatis中的迭代器模...

  • 设计模式系列——迭代器模式

    迭代器模式介绍 迭代器模式是行为模式之一,又称为(Cursor)模式。迭代器模式源于对容器的访问,如List,Ma...

网友评论

      本文标题:Python迭代器模式

      本文链接:https://www.haomeiwen.com/subject/xiomnttx.html