【问题标题】:What is the formal difference between "print" and "return"? [duplicate]\"print\" 和 \"return\" 之间的形式区别是什么? [复制]
【发布时间】:2022-11-14 16:19:20
【问题描述】:

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

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】:

      显示差异的简单示例:

      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.
      

      【讨论】:

        【解决方案4】:

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

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

        返回是函数返回值的主要方式。所有功能将返回一个值,如果没有 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
        

        【讨论】:

          【解决方案5】:

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

          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 中工作,该shell 会自动prints 任何结果以方便您自己。

          【讨论】:

            【解决方案6】:

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

            我建议您阅读有关 Python 的书或阅读教您基础知识的教程,因为这是非常基础的东西。

            【讨论】:

              猜你喜欢
              • 2022-08-06
              • 2022-12-14
              • 2015-10-06
              • 2013-06-09
              • 2020-08-10
              • 1970-01-01
              相关资源
              最近更新 更多