python中返回和打印之间的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3881434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between returns and printing in python?
提问by Chris
In python I don't seem to be understanding the return function. Why use it when I could just print it?
在 python 中,我似乎不理解返回函数。当我可以打印时为什么要使用它?
def maximum(x, y):
if x > y:
print(x)
elif x == y:
print('The numbers are equal')
else:
print(y)
maximum(2, 3)
This code gives me 3. But using return it does the same exact thing.
这段代码给了我3. 但是使用 return 它做同样的事情。
def maximum(x, y):
if x > y:
return x
elif x == y:
return 'The numbers are equal'
else:
return y
print(maximum(2, 3))
So what's the difference between the two? Sorry for the mega noob question!
那么两者有什么区别呢?对不起,超级菜鸟问题!
回答by SilentGhost
What would you do if you need to save printed value? Have a look at good explanation in docsand cf.:
如果您需要保存打印值,您会怎么做?看看文档和 cf.中的好解释:
>>> def ret():
return 42
>>> def pri():
print(42)
>>> answer = pri()
42
>>> print(answer) # pri implicitly return None since it doesn't have return statement
None
>>> answer = ret()
>>> answer
42
It also is no different from returnstatement in any other language.
它也return与任何其他语言的语句没有什么不同。
回答by RichieHindle
For more complex calculations, you need to return intermediate values. For instance:
对于更复杂的计算,您需要返回中间值。例如:
print minimum(3, maximum(4, 6))
You can't have maximumprinting its result in that case.
maximum在这种情况下,您不能打印其结果。
回答by Gareth
Remember that the interactive command line isn't the only place methods will be called from. Methods can also be called by other methods, and in that case printisn't a usable way to pass data between them
请记住,交互式命令行并不是唯一可以调用方法的地方。方法也可以被其他方法调用,在这种情况下print不是在它们之间传递数据的可用方式
回答by Anthony
Honestly, it depends on what you need the function to do. If the function specification state that it will print out max term, then what you have is fine. What generally happens for a method like this is that the method should return the actual value which is larger. In the case they are equal, it doesn't matter which value is returned.
老实说,这取决于您需要该功能做什么。如果函数规范声明它将打印出最大项,那么您所拥有的就可以了。像这样的方法通常发生的情况是该方法应该返回更大的实际值。在它们相等的情况下,返回哪个值并不重要。
回答by t3dodson
The Point
重点
return is not a function. It is a control flow construct (like ifelseconstructs). It is what lets you "take data with you between function calls".
return 不是函数。它是一个控制流构造(如ifelse构造)。它使您可以“在函数调用之间随身携带数据”。
Break down
分解
print: gives the value to the useras an output string.print(3)would give a string'3'to the screen for the user to view. The program would lose the value.return: gives the value to the program. Callers of the function then have the actual data and data type (bool, int, etc...)return 3would have the value 3 put in place of where the function was called.
print: 将值作为输出字符串提供给用户。print(3)会给'3'屏幕一个字符串供用户查看。该程序将失去价值。return: 赋予程序价值。函数的调用者然后拥有实际数据和数据类型(bool、int 等...)return 3将值 3 放在调用函数的位置。
Example Time
示例时间
def ret():
return 3
def pri():
print(3)
4 + ret() # ret() is replaced with the number 3 when the function ret returns
>>> 7
4 + pri() # pri() prints 3 and implicitly returns None which can't be added
>>> 3
>>> TypeError cannot add int and NoneType

