Python 中的 return 是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21696310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does return mean in Python?
提问by user3162541
I searched the whole internet for the meaning of the returnstatement.
我在整个互联网上搜索了return声明的含义。
I know it ends the define statement, but I know it still does something else!
我知道它结束了定义语句,但我知道它仍然在做其他事情!
What else does it do?
它还有什么作用?
回答by aychedee
It returnsthe flow of control to the calling function. It also returnsoutput/results to the calling function.
它returns是调用函数的控制流。它还returns向调用函数输出/结果。
Consider the function below:
考虑下面的函数:
def am_i_wrong(answer):
if answer == 'yes':
return True
else:
return False
You have multiple returns. So returndoesn't simply end the function definition. It instead is the point at which the function returns the result to the caller.
你有多重回报。所以return不会简单地结束函数定义。相反,它是函数将结果返回给调用者的点。
If answeris equal to 'yes' then anything after the if statement (after if and else) is never run because the function has already returned.
Ifanswer等于 'yes' 则 if 语句之后(if 和 else 之后)的任何内容都不会运行,因为该函数已经返回。

