“印刷”和“回归”之间的正式区别是什么?

时间:2011-10-05 16:46:22

标签: python

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

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

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

6 个答案:

答案 0 :(得分:26)

显着不同的东西。想象一下,如果我有这个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语句返回一个函数的值。不带表达式参数的return返回None。从函数末尾开始也会返回None。

答案 1 :(得分:7)

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

这两个陈述具有非常不同的含义,您不应该看到相同的行为。发布您的整个程序,更容易指出与您的区别。

其他人指出

编辑:,如果你在交互式python shell中,你会看到相同的效果(打印的值),但这是因为shell评估表达式并打印输出。

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

答案 2 :(得分:3)

print(如果您使用的是Python 3,则为print())确实打印了关键字后面的任何内容。它还可以做很好的事情,例如使用空格自动连接多个值:

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

否则printprint())将无法从您的程序角度执行任何操作。它不会以任何方式影响控制流程,并且将使用代码块中的下一条指令继续执行:

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

您在屏幕上看到return ed值的原因是因为您可能在交互式Python shell中工作,为了您自己的方便,print自动{{1}}任何结果。

答案 3 :(得分: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 :(得分:3)

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

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

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

考虑这两个程序:

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 :(得分:1)

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

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