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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:25:32  来源:igfitidea点击:

What does return mean in Python?

pythonreturn

提问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 之后)的任何内容都不会运行,因为该函数已经返回。

回答by WeaselFox

The docsexplain fully how a return function works. Its also the first answer in the google query python return...

文档解释返回功能完全是如何工作的。它也是谷歌查询python返回中的第一个答案......