首页 > 解决方案 > 需要澄清这段代码中“返回”和“中断”的功能

问题描述

我在 python3 中编写了这个 python 代码。

def fibonacci(n): #generator function
   a, b, counter = 0, 1, 0
   while True:
      if (counter > n): 
         return
      yield a
      a, b = b, a + b
      counter += 1
f = fibonacci(5) #f is iterator object

在上面的代码中,如果我将 return 替换为“break”语句,那么它仍然可以正常工作。

我正在从这个特定的代码角度寻找更多关于“Return”与“Break”用法的说明。

标签: pythonpython-3.x

解决方案


推荐阅读