迭代器

作者: 书生_Scholar | 来源:发表于2019-10-21 10:04 被阅读0次

1. 判断迭代

# coding=utf-8
"""
1.判断xxxx_obj是否可以迭代
2.在第一步成立的前提下,调用iter函数得到xxx_obj对象的__iter__方法的返回值
3.__iter__方法的返回值是一个迭代器
"""
from collections.abc import Iterable
from collections.abc import Iterator
import time


class Classmate(object):
    def __init__(self):
        self.names = list()

    def add(self, name):
        self.names.append(name)

    def __iter__(self):
        return ClassIterator(self)


class ClassIterator(object):

    def __init__(self, obj):
        self.obj = obj
        self.current_num = 0

    def __iter__(self):
        pass

    def __next__(self):
        if self.current_num < len(self.obj.names):
            ret = self.obj.names[self.current_num]
            self.current_num += 1
            return ret
        else:
            raise StopIteration


classmate = Classmate()
classmate.add("laowang")
classmate.add("laoli")
classmate.add("laosan")

# print("是否可迭代:", isinstance(classmate, Iterable))
#
# classmate_iterator = iter(classmate)
# print("是否迭代器:", isinstance(classmate_iterator, Iterable))
# print(next(classmate_iterator))

for name in classmate:
    print(name)
    time.sleep(1)


2. 简化

# coding=utf-8
"""
1.判断xxxx_obj是否可以迭代
2.在第一步成立的前提下,调用iter函数得到xxx_obj对象的__iter__方法的返回值
3.__iter__方法的返回值是一个迭代器
"""
from collections.abc import Iterable
from collections.abc import Iterator
import time


class Classmate(object):
    def __init__(self):
        self.names = list()
        self.current_num = 0

    def add(self, name):
        self.names.append(name)

    def __iter__(self):
        return self

    def __next__(self):
        if self.current_num < len(self.names):
            ret = self.names[self.current_num]
            self.current_num += 1
            return ret
        else:
            raise StopIteration


classmate = Classmate()
classmate.add("laowang")
classmate.add("laoli")
classmate.add("laosan")
classmate.add("李四")
classmate.add("张流森")
# print("是否可迭代:", isinstance(classmate, Iterable))
#
# classmate_iterator = iter(classmate)
# print("是否迭代器:", isinstance(classmate_iterator, Iterable))
# print(next(classmate_iterator))

for name in classmate:
    print(name)
    time.sleep(1)


3. Fibonacci数列

# coding=utf-8
import time


class Fibonacci(object):

    def __init__(self, num):
        self.current = 0
        self.num = num
        self.a = 0
        self.b = 1

    def __iter__(self):
        return self

    def __next__(self):

        if self.current < self.num:
            ret = self.a
            self.a, self.b = self.b, self.a+self.b
            self.current += 1
            return ret
        else:
            raise StopIteration


fib = Fibonacci(88)

for i, n in enumerate(fib):
    print(i, n)
    time.sleep(1)




相关文章

  • 迭代器

    本节实验我们将为大家讲解迭代器,主要介绍 5 种常见迭代器:输入、输出迭代器,前向逆向迭代器,双向迭代器和随机迭代...

  • 迭代器与生成器

    迭代(iteration)与可迭代(iterable) 迭代器 迭代器协议(iterator protocol) ...

  • 10.迭代器与生成器

    一、迭代器 1). 迭代器概述 类比Java中的迭代器,参考迭代器模式https://www.jianshu.co...

  • 9 ES6 迭代器、生成器

    1、Iterator迭代器 ->遵循迭代模式思想实现,分为内部迭代器、外部迭代器,聚焦点:添加迭代接口 (1).迭...

  • C++boolan part3_week4

    1. 迭代器 1.1 迭代器的种类 使用随机访问迭代器的容器:array, vector,deque使用双向迭代器...

  • 迭代器模式

    内部迭代器和外部迭代器 简单地讲,内部迭代器就是类似 forEach 的迭代器,其内部迭代逻辑已经制定好,只要传入...

  • vector erase的用法

    正向迭代器: 反向迭代器:

  • 10 python中的迭代器和生成器

    1 迭代器 遵循了迭代器协议的对象为迭代器,即内部调用iter()方法转化成的对象为成迭代器。 1.1 迭代器的特...

  • 第7章迭代器模式

    第7章迭代器模式 7.1 jQuery中的迭代器 7.2 实现自己的迭代器 7.3 内部迭代和外部迭代 1内部迭代...

  • 我们来讲讲Python的迭代器

    迭代器 迭代器的作用:迭代器的作用就是迭代,可以让不是序列但是表现出序列行为,例如迭代字典的键,文件的行 迭代器的...

网友评论

    本文标题:迭代器

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