【发布时间】:2021-05-04 20:49:07
【问题描述】:
为什么会这样?
def nextSquare():
i = 1;
# An Infinite loop to generate squares
while True:
yield i*i
i += 1 # Next execution resumes
# from this point
# Driver code to test above generator
# function
for num in nextSquare():
if num > 100:
break
print(num)
但不是这个?
def nextSquare():
i = 1;
# An Infinite loop to generate squares
while True:
yield i*i
i += 1 # Next execution resumes
# from this point
# Driver code to test above generator
# function
for num in 1:
if num > 100:
break
print(num)
nextsquare每次运行都会返回一个整数,那么nextsquare第一次返回的区别是什么
for num in nextSquare():
和
for num in 1:
【问题讨论】:
-
这是一个非常抽象的问题。你想要一个解决方案,还是一个 python 实现和设计权衡讨论?
-
这能回答你的问题吗? What does the "yield" keyword do?
标签: python-3.x for-loop yield