迭代器协议

class Foo:
    def __init__(self,n):
        self.n=n
    def __iter__(self):
        return self

    def __next__(self):
        if self.n == 13:
            raise StopIteration('终止了')
        self.n+=1
        return self.n

# l=list('hello')
# for i in l:
#     print(i)
f1=Foo(10)
# print(f1.__next__())
# print(f1.__next__())
# print(f1.__next__())
# print(f1.__next__())

for i in f1:  # obj=iter(f1)------------>f1.__iter__()
     print(i)  #obj.__next_()

 

相关文章:

  • 2022-12-23
  • 2021-12-27
  • 2021-08-06
  • 2021-05-22
  • 2021-07-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-02-01
  • 2022-12-23
  • 2021-05-25
  • 2021-10-01
  • 2021-09-23
相关资源
相似解决方案