python - Python函数在return语句后召回自己?
问题描述
这个问题比较学术。我在python中遇到了以下行为:
此代码:
def dosth():
print("new")
for n in range(0,10):
return False
dosth()
实际上给了我 10 张照片而不是一张。那么函数在return语句之后是否因为没有break而递归调用自己呢?如果我在 return 语句之前休息一下,我仍然会得到 2 次打印,而不是只有 1 次。有人可以解释这种行为吗
解决方案
I only see one new
printed.
It also appears that you're a missing a colon :
at the end of your loop for n in range(0,10)
.
def dosth():
print("new")
for n in range(0,10):
return False
dosth()
# prints 'new' once...
推荐阅读
- python - Raspberry PI 零 w PIR 发送到 Telethon
- python - 从一个分割列号创建多个数据框
- mongodb - 我需要哪些服务来设置后端?
- docker - 我的 Dockerize go 应用程序不工作。错误:/bin/sh: ./crawler: 未找到
- react-native - 有没有办法让你在一个完全离线的移动应用程序中拥有一个后端反应原生
- reactjs - 如何在 Onblur 操作中获取 codemirror 中的代码
- python - 使用 discord.py 将 wait_for 中的消息转换为频道
- javascript - 如何将一个功能用于 2 个组件的变体?
- python - Python/Keras:使用张量流的 LeakyRelu
- python - 在python中将CSV流式传输到镶木地板