【问题标题】:What does return do? Nothing is ever returned退货有什么作用?什么都没有返回
【发布时间】:2013-09-28 12:23:55
【问题描述】:

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

也许有人可以用简单的方式解释什么返回? 我已经编写了几个有用的(对我而言)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。

【问题讨论】:

  • a = sqrt(25); print(a);

标签: python return-value


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    return 可以从函数中获取一些输出/结果。稍后在代码中将此值分配给变量:

    a = sqrt(25)
    

    【讨论】:

      【解决方案3】:

      您的教科书希望您尝试interactive interpreter 中的内容,它会在您输入值时显示您的值。这是一个外观示例:

      $ 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)。如果您的函数不包含returnyield,则此值为None,解释器会忽略它,但在这种情况下,sqrt 返回一个自动打印的数字(并存储在名为_ 的变量中)。在脚本中,您可以将最后一行替换为 print sqrt(25) 以获取终端的输出,但返回值的有用之处在于您可以进行进一步处理,例如 root=sqrt(25)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"
      

      【讨论】:

        【解决方案4】:

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

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

        因此,如果您将 def gimmieFive(): return 5 更改为 def gimmieFive(): 5,那么运行 x = gimmieFive() 的人将拥有 x == None,而不是 x == 5。 5 就被扔掉了 gimmieFive 退出。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-19
          • 1970-01-01
          相关资源
          最近更新 更多