#!/usr/local/python/bin/python3
"""
一个迭代器的例子
"""

class exsample(object):
    """
    exsample 类实现迭代功能
    __iter__返回一个迭代器
    __next__定义每一次迭代要返回的值
    """
    def __init__(self,counter=10):
        self.index=0
        self.counter=counter

    def __iter__(self):
        return self

    def __next__(self):
        if self.index <self.counter:
            self.index=self.index+1
            return self.index -1
        else:
            raise StopIteration()


if __name__=="__main__":
    for x in exsample():
        print(x)

 

[root@workstudio tmp]# ./main.py 
0
1
2
3
4
5
6
7
8
9

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-27
  • 2021-11-10
  • 2021-07-31
  • 2022-12-23
  • 2022-12-23
  • 2021-05-05
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-01-09
  • 2021-08-14
  • 2022-12-23
  • 2021-06-30
  • 2022-12-23
相关资源
相似解决方案