python - 需要澄清这段代码中“返回”和“中断”的功能
问题描述
我在 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”用法的说明。
解决方案
推荐阅读
- python - 是否可以在字符串中添加“任何字母”?
- javascript - 如何将表单重置为其默认值?
- java - 如果超类具有有界泛型,具体类的泛型应该是什么
- java - 识别黑莓手机
- python - PySpark - 通过函数 .to_date() 和 .csv() 模块以不同方式导入的日期 0000.00.00
- sql - oracle sql列值从..到
- wordpress - elementor插件右空间
- c# - 设置 SelectList 选项标签的值
- wso2 - 如何在服务器上实现 wso2?
- c# - 当 IMG 图像不存在时,C# WebBrowser.Print() 不打印特定的 HTML 文件