1

我已经学习 Python 好几天了。但是,我不明白回报。我从教科书和网上阅读了一些解释;他们没有帮助!

也许有人可以用简单的方式解释 return 的作用?我已经编写了几个有用的(对我而言)Python 脚本,但我从未使用过 return,因为我不知道它的作用。

你能提供一个简单的例子来说明为什么应该使用 return 吗?

它似乎也什么都不做:

def sqrt(n):
    approx = n/2.0
    better = (approx + n/approx)/2.0
    while better != approx:
        approx = better
        better = (approx + n/approx)/2.0
    return approx

sqrt(25)

我的教科书告诉我:“尝试使用 25 作为参数调用此函数,以确认它返回 5.0。”

我知道如何检查的唯一方法是使用print。但我不知道这是否是他们正在寻找的。这个问题只是说用 25 调用。它没有说在代码中添加更多内容以确认它返回 5.0。

4

4 回答 4

5

return从函数返回一个值:

def addseven(n):
    return n + 7

a = 9
b = addseven(a)
print(b)        # should be 16

它也可以用于退出函数:

def addseventosix(n):
    if n != 6:
        return
    else:
        return n + 7

但是,即使您return在函数中没有语句(或者您在没有指定要返回的值的情况下使用它),该函数仍然会返回一些东西 - None

def functionthatisuseless(n):
    n + 7

print(functionthatisuseless(8))        # should output None

有时您可能希望从一个函数返回多个值。但是,您不能有多个return语句 - 控制流在第一个语句之后离开函数,因此在它之后的任何内容都不会被执行。在 Python 中,我们通常使用一个元组,以及元组解包:

def addsevenandaddeight(n):
    return (n+7, n+8)        # the parentheses aren't necessary, they are just for clarity

seven, eight = addsevenandaddeight(0)
print(seven)        # should be 7
print(eight)        # should be 8

return语句允许您根据其他函数的结果调用函数:

def addseven(n):
    return n+7

def timeseight(n):
    return n*8

print(addseven(timeseight(9))

# what the intepreter is doing (kind of):
# print(addseven(72))    # 72 is what is returned when timeseight is called on 9
# print(79)
# 79
于 2013-09-28T12:26:47.470 回答
2

您需要添加一个print或将整个内容写入交互式解释器以查看返回值。

return可以从函数中获得一些输出/结果。在代码后面的这个值,你必须将它分配给一个变量:

a = sqrt(25)
于 2013-09-28T12:28:35.337 回答
0

您的教科书希望您在交互式解释器中进行尝试,它会在您输入值时向您显示值。这是一个外观示例:

$ python
Python 2.7.5+ (default, Sep 17 2013, 17:31:54)
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def sqrt(n):
...     approx = n/2.0
...     better = (approx + n/approx)/2.0
...     while better != approx:
...         approx = better
...         better = (approx + n/approx)/2.0
...     return approx
...
>>> sqrt(25)
5.0
>>>

这里的关键是表达式和​​语句之间的区别。def是一个语句,不会产生任何结果。sqrtdef块定义的,是一个函数;和函数总是产生一个返回值,这样它们就可以在表达式中使用,比如sqrt(25). 如果你的函数不包含return或者yield这个值是None,解释器会忽略它,但在这种情况下 sqrt 返回一个自动打印的数字(并存储在一个名为 的变量中_)。在脚本中,您可以将最后一行替换为print sqrt(25)以将输出发送到终端,但返回值的有用之处在于您可以进行进一步处理,例如root=sqrt(25)or print sqrt(25)-5

如果我们要运行与脚本完全相同的行,而不是在交互模式下,则没有隐式打印。该行sqrt(25)被接受为表达式的语句,这意味着它已被计算 - 但随后该值被简单地丢弃。它甚至没有进入_(相当于计算器的 Ans 按钮)。通常我们将它用于导致副作用的函数,例如quit()导致 Python 退出的函数。

顺便说一句,print在 Python 2 中是一个语句,但在 Python 3 中是一个函数。这就是为什么越来越多地使用括号。

这是一个依赖sqrt(在本例中是 Python 自己的版本)返回值的脚本:

from math import sqrt
area = float(raw_input("Enter a number: "))
shortside = sqrt(area)
print "Two squares with the area", area, "square meters,",
print "placed side to side, form a rectangle", 2*shortside, "meters long",
print "and", shortside, "meters wide"
于 2013-09-28T14:56:44.330 回答
0

关键字是return退出函数并返回一个值。要让函数返回值,请使用return语句。

正如 Charlie Duffy评论的那样,当您不在return函数中的任何地方使用时,Python 的隐式返回值为None. 换句话说,如果你不告诉一个函数它应该返回一些东西,那么它就会None返回什么。

所以如果你改成def gimmieFive(): return 5just def gimmieFive(): 5,那么跑的人x = gimmieFive()就会有 x == None,而不是x == 5。当 gimmieFive 退出时,5 就被扔掉了。

于 2020-03-31T02:28:46.867 回答