#! /usr/bin/env python # -*- coding: utf-8 -*- # __author__ = "Deakin" # Email: [email protected] # Date: 2018/3/7 def fib(limit): n,a,b=0,0,1 while n < limit: #print(b) yield b #把print替换成yield就把函数变成了生成器 a,b=b,a+b n+=1 return ‘end‘ f=fib(10) print(f) print(f.__next__()) print(‘插入符‘) #生成器可以保存列表的使用位置 print(f.__next__()) print(f.__next__()) for i in f: print(i) 打印结果: <generator object fib at 0x000002518DED8C50> 1 插入符 1 2 3 5 8 13 21 34 55