python 迭代器示例Fibonacci

# -*- coding:utf-8 -*-
__metaclass__ = type
class Fibs:
  def __init__(self):
    self.a = 0
    self.b = 1
  def __next__(self):
    self.a,self.b = self.b,self.a+self.b
    return self.a
  def __iter__(self):
    return self

if __name__ == "__main__":
  fibs = Fibs()
  for f in fibs:
    if f > 100:
      break
    else:
      print(f)

你可能感兴趣的:(python 迭代器示例Fibonacci)