首先,先直观上理解一下两种返回方式的区别:python中yield的用法详解——最简单,最清晰的解释
然后,概括的理解一下:python-复盘-yield & return区别 yield 能和 for 循环共同使用
最后,两种返回值,在 for 循环上的区别:
import torch
def fun():
for i
in range(3):
a
= torch
.tensor
([[1,2]])
b
= torch
.tensor
([[3,4,5]])
return a
,b
i
=0
for a
in fun
():
i
+= 1
print('step:',i
)
print(a
)
import torch
def fun():
for i
in range(3):
a
= torch
.tensor
([[1,2]])
b
= torch
.tensor
([[3,4,5]])
yield a
,b
i
=0
for a
in fun
():
i
+= 1
print('step:',i
)
print(a
)