一:生成器#生成器:为了节约内存,拿到内存地址一边循环一边计算
a=(x*2 for x in range(1,8))
print(a)
print(next(a))
print(next(a))
<generator object <genexpr> at 0x00934670>
2
4
#函数中有yield,该函数就是生成器,该函数需要赋值给一个对象(内存地址赋值给引用对象),yield类似return结束循环并且返回值,下次next()执行时,从yield后开始执行
def create_num():
print('start')
a,b=0,1
for i in range(5):
print('----1----')
yield b
print('----2----')
a,b=b,a+b
print('-----3----')
print('-----stop-----')
a=create_num()
print(a)
print(next(a))
print()
print(next(a))
<generator object create_num at 0x00934E30>
start
----1----
1
----2----
-----3----
----1----
1
标签:闭包,10,生成器,yield,next,----,-----,print From: https://www.cnblogs.com/vip01/p/16928654.html