【问题标题】:What is the formal difference between "print" and "return"? [duplicate]“打印”和“返回”之间的正式区别是什么? [复制]
【发布时间】:2011-12-01 15:53:01
【问题描述】:

假设我定义了一个简单的函数,它将显示传递给它的整数:

def funct1(param1):
    print(param1)
    return(param1)

输出将是相同的,但我知道当在函数中使用return 语句时,可以再次使用输出。否则不能使用print 语句的值。但是我知道这不是正式的定义,谁能给我一个好的定义?

【问题讨论】:

  • 你在用python交互提示吗?
  • print 打印,return 返回。
  • 你从哪里得知return 会输出任何东西?

标签: python


【解决方案1】:

截然不同的事情。想象一下,如果我有这个 python 程序:

#!/usr/bin/env python

def printAndReturnNothing():
    x = "hello"
    print(x)

def printAndReturn():
    x = "hello"
    print(x)
    return x

def main():
    ret = printAndReturn()
    other = printAndReturnNothing()

    print("ret is: %s" % ret)
    print("other is: %s" % other)

if __name__ == "__main__":
    main()

你期望的输出是什么?

hello
hello
ret is : hello
other is: None

为什么?

为什么?因为print将其参数/表达式转储到标准输出,所以在我编写的函数中,print会输出x的值,即hello

  • printAndReturn 将返回x 给方法的调用者,所以:

    ret = printAndReturn()

ret 将具有与x 相同的值,即"hello"

  • printAndReturnNothing 不返回任何内容,所以:

    other = printAndReturnNothing()

other 实际上变成了None,因为这是 python 函数的默认返回。 Python 函数总是返回一些东西,但如果没有声明 return,函数将返回 None


资源

通过 python 教程将向您介绍这些概念:http://docs.python.org/tutorial

这里有一些关于函数的内容来自 python 的教程:http://docs.python.org/tutorial/controlflow.html#defining-functions

与往常一样,此示例演示了一些新的 Python 功能:

return 语句从函数返回值。没有表达式参数的返回返回无。离开函数的末尾也返回 None。

【讨论】:

    【解决方案2】:

    使用print(),您将向标准输出显示param1 的值,而使用return,您将向调用者发送param1

    这两个语句的含义非常不同,您不应该看到相同的行为。发布您的整个程序,您会更容易指出不同之处。

    编辑:正如其他人所指出的,如果您在交互式 python shell 中,您会看到相同的效果(打印值),但发生这种情况是因为 shell 评估表达式并打印它们的输出。

    在这种情况下,带有return 语句的函数被评估为return 本身的参数,因此返回值被回显。不要让交互式 shell 欺骗您! :)

    【讨论】:

      【解决方案3】:

      我将从一个基本的解释开始。 print 只是向人类用户显示一个字符串,表示计算机内部正在发生的事情。计算机无法使用该打印。 return 是函数返回值的方式。人类用户通常看不到这个值,但计算机可以在其他功能中使用它。

      更广泛地说,print 不会以任何方式影响功能。它只是为了人类用户的利益而存在。它对于理解程序的工作原理非常有用,并且可以在调试中用于检查程序中的各种值而不会中断程序。

      return 是函数返回值的主要方式。所有函数都会返回一个值,如果没有返回语句(或yield但别担心),它会返回 没有。然后,函数返回的值可以进一步用作传递给另一个函数的参数,存储为变量,或者只是为了人类用户的利益而打印。

      考虑这两个程序:

      def function_that_prints():
          print "I printed"
      
      def function_that_returns():
          return "I returned"
      
      f1 = function_that_prints()
      f2 = function_that_returns()
      print "Now let us see what the values of f1 and f2 are"
      print f1
      print f2
      

      【讨论】:

        【解决方案4】:

        显示差异的简单示例:

        def foo():
            print (5)
        
        def bar():
            return 7
        
        x = foo() 
        y = bar()
        
        print (x) 
        # will show "None" because foo() does not return a value
        print (y) 
        # will show "7" because "7" was output from the bar() function by the return statement.
        

        【讨论】:

          【解决方案5】:

          print(或print(),如果您使用的是 Python 3)正是这样做的——打印关键字后面的任何内容。它还会做一些好事,比如自动用空格连接多个值:

          print 1, '2', 'three'
          # 1 2 three
          

          否则,从您的程序的角度来看,print (print()) 将无能为力。它不会以任何方式影响控制流,并且会从代码块中的下一条指令继续执行:

          def foo():
              print 'hello'
              print 'again'
              print 'and again'
          

          另一方面,return(不是return())旨在立即中断控制流并退出当前函数并将指定的值返回给调用您的函数的调用者。它总是会这样做,而且只会这样做。 return 本身不会导致任何内容打印到屏幕上。即使您没有指定返回值,也会返回隐式 None。如果您完全跳过 return,隐含的 return None 仍会在您的函数结束时发生:

          def foo(y):
              print 'hello'
              return y + 1
              print 'this place in code will never get reached :('
          print foo(5)
          # hello
          # 6
          
          def bar():
              return # implicit return None
          print bar() is None
          # True
          
          def baz(y):
              x = y * 2
              # implicit return None
          z = baz()
          print z is None
          # True
          

          您看到returned 值打印到屏幕上的原因可能是因为您可能正在交互式Python shell 中工作,为了您自己的方便,它会自动prints 任何结果。

          【讨论】:

            【解决方案6】:

            仅在交互式终端中的输出相同。当你正常执行程序时,结果会完全不同。

            我建议你读一本关于 Python 的书或者读一个教你基础知识的教程,因为这是非常基础的东西。

            【讨论】:

              猜你喜欢
              • 2011-04-22
              • 2014-04-09
              • 2017-01-05
              • 2022-08-17
              • 2016-06-28
              • 2020-10-08
              • 2022-11-14
              • 1970-01-01
              相关资源
              最近更新 更多