是否建议在python函数中使用print语句而不是返回 [英] Is it advisable to use print statements in a python function rather than return

查看:87
本文介绍了是否建议在python函数中使用print语句而不是返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我说我有以下功能:

def function(a)
    c = a+b
    print(c)

建议在函数中使用print语句显示输出,而不是在最后放置return语句并使用 print(function(a))?

Is it advisable to use the print statement in the function to display output rather than placing a return statement at the end and using the print(function(a))?

如果我在函数中同时使用print语句和return语句来显示相同​​的输出,还会有什么含义?假设我需要显示 c 的答案,然后在其他地方使用 c 的值.这会违反任何编码约定吗?

Also what implications would there be if I used both a print statement and a return statement in a function to display the same output? Lets imagine I need to show the answer for c and then use the value of c somewhere else. Does this break any coding conventions?

因此,问题的重点不是 print return 之间的区别,而是将其用于同一功能和如果它可能对程序有影响.例如:

So the highlight of the question isn't the difference between print and return, but rather if it is considered a good style to use both in the same function and if it has a possible impact on a program. For example in:

 def function(a)
    c = a+b
    print(c)
    return c
 value = function
 print(value)

结果是否为两个 c ?假设 c = 5 ;因此,输出是否为(?):

Would the result be two c's? Assume c = 5; therefore, would the output be(?):

5
5

推荐答案

print return 解决了两个完全不同的问题.当交互运行琐碎的示例时,它们会出现来做相同的事情,但是它们是完全不同的.

print and return solve two completely different problems. They appear to do the same thing when running trivial examples interactively, but they are completely different.

如果您确实只想打印结果,请使用 print .如果需要结果作进一步计算,请使用 return .相对来说,很少使用这两种方法,除了在调试阶段,如果不使用调试器,打印语句可以帮助您了解发生了什么.

If you indeed just want to print a result, use print. If you need the result for further calculation, use return. It's relatively rare to use both, except during a debugging phase where the print statements help see what's going on if you don't use a debugger.

根据经验,我认为最好避免在函数中添加 print 语句,除非该函数的显式目的是打印出某些内容.

As a rule of thumb I think it's good to avoid adding print statement in functions, unless the explicit purpose of the function is to print something out.

在所有其他情况下,函数应返回一个值.然后,调用该函数的函数(或人员)可以决定打印它,将其写入文件,将其传递给另一个函数,等等.

In all other cases, a function should return a value. The function (or person) that calls the function can then decide to print it, write it to a file, pass it to another function, etc.

因此,问题的重点不是印刷与印刷之间的区别.返回,而是在相同的功能及其对程序的影响.

So the highlight of the question isnt the difference between print and return but rather if it is considered good style to use both in the same function and its possible impact on a program.

这不是好风格,也不是坏风格.除了最终会打印很多可能不需要打印的内容外,对程序没有任何重大影响.

It's not good style, it's not bad style. There is no significant impact on the program other than the fact you end up printing a lot of stuff that may not need to be printed.

如果您需要同时打印和返回值的函数,则完全可以接受.通常,这种事情很少在编程中完成.它可以追溯到功能设计的概念.如果要打印,则通常没有意义返回值,如果要返回值,则通常没有意义,因为调用方可以根据需要进行打印.

If you need the function to both print and return a value, it's perfectly acceptable. In general, this sort of thing is rarely done in programming. It goes back to the concept of what the function is designed to do. If it's designed to print, there's usually no point in returning a value, and if it's designed to return a value, there's usually no point in printing since the caller can print it if it wants.

这篇关于是否建议在python函数中使用print语句而不是返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆