问题描述
我在整个互联网上搜索了return
语句的含义.
I searched the whole internet for the meaning of the return
statement.
我知道它结束了define语句,但我知道它还有别的作用!
I know it ends the define statement, but I know it still does something else!
它还有什么作用?
推荐答案
它返回
控制流给调用函数.它还返回
输出/结果给调用函数.
It returns
the flow of control to the calling function. It also returns
output/results to the calling function.
考虑下面的函数:
def am_i_wrong(answer):
if answer == 'yes':
return True
else:
return False
您有多次退货.所以 return
不是简单地结束函数定义.相反,它是函数将结果返回给调用者的点.
You have multiple returns. So return
doesn't simply end the function definition. It instead is the point at which the function returns the result to the caller.
如果 answer
等于 'yes',则 if 语句之后(if 和 else 之后)的任何内容都不会运行,因为该函数已经返回.
If answer
is equal to 'yes' then anything after the if statement (after if and else) is never run because the function has already returned.
这篇关于Python 中的 return 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!