退货声明的目的是什么?

Hit*_*mar 79 python printing return

什么是return语句的简单基本解释,如何在Python中使用它?

它和print声明有什么区别?

Nat*_*hes 94

print()函数在控制台中写入,即"打印"字符串.该return语句使您的函数退出并将值传回其调用者.功能一般来说就是接受输入并返回一些东西.return函数准备好向其调用者返回值时使用该语句.

例如,这是一个利用两个print()和的函数return:

def foo():
    print("hello from inside of foo")
    return 1
Run Code Online (Sandbox Code Playgroud)

现在您可以运行调用foo的代码,如下所示:

if __name__ == '__main__':
    print("going to call foo")
    x = foo()
    print("called foo")
    print("foo returned " + str(x))
Run Code Online (Sandbox Code Playgroud)

如果您将此作为脚本(例如.py文件)而不是Python解释器运行,您将获得以下输出:

going to call foo
hello from inside foo
called foo   
foo returned 1
Run Code Online (Sandbox Code Playgroud)

我希望这更清楚.解释器将返回值写入控制台,这样我就可以看出为什么有人会感到困惑.

这是解释器的另一个例子,它表明:

>>> def foo():
...     print("hello from within foo")
...     return 1
...
>>> foo()
hello from within foo
1
>>> def bar():
...   return 10 * foo()
...
>>> bar()
hello from within foo
10
Run Code Online (Sandbox Code Playgroud)

您可以看到,在foo()调用时bar(),1不会写入控制台.相反,它用于计算从中返回的值bar().

print()是一个导致副作用的函数(它在控制台中写入一个字符串),但执行将继续执行下一个语句.return导致函数停止执行并将值传回任何调用它的函数.


Kim*_*ais 23

我认为这本词典是你最好的参考

退货打印

简而言之:

print 生成文本时,return 返回回复函数的调用者


Wes*_*Wes 21

将print语句视为导致副作用,它会使您的函数向用户写入一些文本,但它不能被其他函数使用.

我将尝试用一些例子和维基百科的一些定义来更好地解释这一点.

以下是维基百科中函数的定义

在数学中,函数将一个数量,函数的参数(也称为输入)与另一个数量(函数的值)(也称为输出)相关联.

这点考虑一下吧.当你说这个函数有一个值时,它是什么意思?

这意味着您实际上可以用正常值替换函数的值!(假设两个值是相同类型的值)

你为什么要这么问?

那些可能接受相同类型的值作为输入的其他函数呢?

def square(n):
    return n * n

def add_one(n):
    return n + 1

print square(12)

# square(12) is the same as writing 144

print add_one(square(12))
print add_one(144)
#These both have the same output
Run Code Online (Sandbox Code Playgroud)

对于仅依赖于其输入来产生其输出的函数,有一个奇特的数学术语:参考透明度.同样,来自维基百科的定义.

参考透明度和参考不透明性是计算机程序的一部分的属性.如果表达式可以在不改变程序行为的情况下用其值替换,则表示该表达式是引用透明的

如果你刚接触编程,可能有点难以理解这意味着什么,但我认为你会在经过一些实验后得到它.一般来说,你可以在函数中执行打印等操作,最后也可以使用return语句.

请记住,当您使用return时,您基本上是在说"对此函数的调用与写入返回的值相同"

如果您拒绝放入自己的Python,它实际上会为您插入一个返回值,它被称为"无",它是一种特殊类型,只是没有任何意义,或者为null.


She*_*hah 12

在python中,我们开始用"def"定义一个函数,通常但不一定,用"return"结束函数.

变量x的函数表示为f(x).这个功能有什么作用?假设,此函数将x加2.所以,f(x)= x + 2

现在,这个函数的代码将是:

def A_function (x):
    return x + 2
Run Code Online (Sandbox Code Playgroud)

定义函数后,您可以将其用于任何变量并获取结果.如:

print A_function (2)
>>> 4
Run Code Online (Sandbox Code Playgroud)

我们可以稍微改写代码,例如:

def A_function (x):
    y = x + 2
    return y
print A_function (2)
Run Code Online (Sandbox Code Playgroud)

这也会给出"4".

现在,我们甚至可以使用此代码:

def A_function (x):
    x = x + 2
    return x
print A_function (2)
Run Code Online (Sandbox Code Playgroud)

这也会给出4.看,返回旁边的"x"实际上意味着(x + 2),而不是"A_function(x)"的x.

我想从这个简单的例子中,你会理解return命令的含义.


BoR*_*Ris 11

上面没有讨论过的案例.
返回语句可以终止你达到执行的end.The流之前立即返回给调用者的函数的执行.

在第9行:

def ret(n):
    if n > 9:
         temp = "two digits"
         return temp     #Line 4        
    else:
         temp = "one digit"
         return temp     #Line 8
    print("return statement")
ret(10)
Run Code Online (Sandbox Code Playgroud)

执行条件语句后,函数ret()返回而终止(第9行).因此,print("return语句")不会被执行.

two digits   
Run Code Online (Sandbox Code Playgroud)

返回语句后出现的代码或未达到控制流的位置是死代码.

返回值
在第4行和第8行中,返回语句用于在执行条件后返回临时变量的值.

显示打印返回之间的区别:

def ret(n):
    if n > 9:
        print("two digits")
        return "two digits"           
    else :
        print("one digit")
        return "one digit"        
ret(25)
Run Code Online (Sandbox Code Playgroud)


cwa*_*ole 9

return 表示"从此函数输出此值".

print 表示"将此值发送到(通常)stdout"

在Python REPL中,函数返回默认输出到屏幕(这与print不完全相同).

这是一个打印的例子:

>>> n = "foo\nbar" #just assigning a variable. No output
>>> n #the value is output, but it is in a "raw form"
'foo\nbar'
>>> print n #the \n is now a newline
foo
bar
>>>
Run Code Online (Sandbox Code Playgroud)

这是一个回归的例子:

>>> def getN():
...    return "foo\nbar"
...
>>> getN() #When this isn't assigned to something, it is just output
'foo\nbar'
>>> n = getN() # assigning a variable to the return value. No output
>>> n #the value is output, but it is in a "raw form"
'foo\nbar'
>>> print n #the \n is now a newline
foo
bar
>>>
Run Code Online (Sandbox Code Playgroud)


Lon*_*Rob 8

只是为了增加@Nathan Hughes的优秀答案:

return陈述可以用作一种控制流程.通过return在函数中间放置一个(或多个)语句,我们可以说:"停止执行此函数.我们要么得到了我们想要的东西,要么出错了!"

这是一个例子:

>>> def make_3_characters_long(some_string):
...     if len(some_string) == 3:
...         return False
...     if str(some_string) != some_string:
...         return "Not a string!"
...     if len(some_string) < 3:
...         return ''.join(some_string,'x')[:,3]
...     return some_string[:,3]
... 
>>> threechars = make_3_characters_long('xyz')    
>>> if threechars:
...     print threechars
... else:
...     print "threechars is already 3 characters long!"
... 
threechars is already 3 characters long!
Run Code Online (Sandbox Code Playgroud)

有关这种使用方式的更多建议,请参阅Python指南的代码样式部分return.


And*_*ndy 5

我认为一个非常简单的答案在这里可能有用:

return使值(通常是一个变量)可供调用者使用(例如,由使用的函数所在的函数存储return)。如果没有return,调用者将无法存储/重用您的值或变量。

print 打印到屏幕上,但不会使调用者可以使用该值或变量。

(完全承认越彻底的答案越准确。)


归档时间:

查看次数:

557935 次

最近记录:

6 年,1 月 前