标签:a+b 生成 turn print 记录 return 没有 取值 test
def add(a, b):
return a+b
def test():
for r_i in range(4):
yield r_i
g = test()
for n in [2,10]:
g = (add(n, i) for i in g)
print(list(g)) # [20, 21, 22, 23]
def add(a, b):
return a+b
‘‘‘生成器函数‘‘‘
def test():
for r_i in range(4):
yield r_i
g = test() # 获取生成器,未取值 0,1,2,3 懒
for n in [2,10]:
g = (add(n, i) for i in g)
‘‘‘
当n =2
g = (add(n, i) for i in g) 此时只是记录,并没有取值
n=10
g = (add(n, i) for i in (add(n, i) for i in g))
记录此时的g, 当list(g)再取值,此时n=10,把10带入
list(g) 0,1,2,3
g = (add(10, i) for i in (add(10, i) for i in [0,1,2,3]))
‘‘‘
print(list(g)) # [20, 21, 22, 23]
标签:a+b 生成 turn print 记录 return 没有 取值 test
原文地址:https://www.cnblogs.com/bing-123/p/12048444.html