简体   繁体   English

返回结果并在函数结束时打印它之间的最佳效果是什么?

[英]What's the best between return the result and print it at the end of the function?

Assume we define a function and then end it with: 假设我们定义一个函数,然后用以下结尾:

def function():
     # ...
     return result
# To see the result, we need to type:
print(function())

Another option is to end a function with a print : 另一种选择是使用print结束一个函数:

def function():
     # ...
     print(result)

# no need of print at the call anymore so
function()

Question: May I end function with a return statement, get it via function() or not? 问题:我可以用return语句结束函数,通过function()获取它吗?
I mean I don't care whether function saves the result or not. 我的意思是我不关心函数是否保存结果。 However the function can have several different results, ie I need to quit the loop at some point. 但是该函数可以有几个不同的结果,即我需要在某个时刻退出循环。 The main idea is to get the output on the screen. 主要思想是在屏幕上获得输出。
So, let me know please whether my variant is OK or it's not 'elegant' coding. 所以,请让我知道我的变体是否正常,或者它不是'优雅'编码。 Thanks! 谢谢!

If you return print(result) , you're essentially doing return None because print() returns None . 如果return print(result) ,则基本上return None因为print()返回None So that doesn't make much sense. 所以这没有多大意义。

I'd say it's much cleaner to return result and have the caller decide whether to print() it or do whatever else with it. 我会说return result更清晰,并让调用者决定是print()它还是用它做任何其他事情。

The most cleaner way is with the return statement . 最简洁的方法是使用return语句 In general, a function returns a result, and this result can be processed after by another algorithm. 通常,函数返回结果,并且此结果可以在其他算法之后处理。 Maybe you don't need to get the result in a variable in your case, but imagine you do in 1 week, month... 也许你不需要在你的情况下得到一个变量的结果,但想象你在一周,一个月做...

The best way is to delegate the print at the main program itself. 最好的方法是将print委托给主程序本身。 You'll manage data in your program more easily and, as I said, you can chain functions. 您将更轻松地管理程序中的数据,正如我所说,您可以链接功能。

Imagine two functions a(arg) and b(arg) that returns two calculations. 想象一下两个函数a(arg)b(arg)返回两个计算。 With the print statement in the b function, you'll not be able to do this: 使用b函数中的print语句,您将无法执行此操作:

a(b(10))

because a will receive a None value in argument (because functions returns None by default, which is the case with the print statement at the end). 因为a将在参数中接收None值(因为默认情况下函数返回None ,最后是print语句的情况)。

TL;DR : Follow this pattern most of the time: TL; DR :大部分时间都遵循这种模式:

def get_full_name(arg1, arg2, ...):
    # Do cool stuff
    return res   # <- result of the function


print get_full_name('foo', 'bar')
full_name = get_full_name('Maxime', 'Lorant')
print some_other_function(full_name)
# etc.

The print function will not return anything; print功能不会返回任何内容; it will only print the stuff as output to the console. 它只会打印输出到控制台的东西。 So returning the return value from the print function does not really make much sense. 因此从print函数返回返回值并没有多大意义。 You are returning nothing by definition. 你没有按照定义返回任何内容。

So unless you want to end the function early, there is no reason why you should use return in this case. 所以,除非你想提前结束这个功能,否则你没有理由在这种情况下使用return Just printing the result (without using return ) is fine and a lot clearer. 只打印结果(不使用return )很好,更清晰。

Also, if you want to return the actual content the print function prints, then you should just do that instead and leave it to the caller to decide whether to print it or not. 此外,如果要返回print功能打印的实际内容,则应该执行此操作,然后将其留给调用者以决定是否打印。

If you want to use print(function()) (which to me sounds the most reasonable here), then just return the result from the function: 如果你想使用print(function()) (这对我来说听起来最合理),那么只需返回函数的结果:

def function ():
    # ...
    return result

If you only care about printing it you should just do: 如果您只关心打印它,您应该这样做:

print(result)

at the end of the function, but generally if you are trying to use the result then just return it and then you can use the result and print it 在函数结束时,但通常如果您尝试使用结果然后只返回它然后您可以使用结果并打印它

you probably shouldn't do return print(result) just do either or print it outside the function 你可能不应该做return print(result)只是做或者在函数外面打印它

if you use return print(result) you aren't returning anything so its pointless 如果你使用return print(result)你没有返回任何东西,所以它毫无意义

No you can't. 不,你不能。 When you call a function the value return is print() (not value of result) 当你调用一个函数时,返回的值是print()(不是结果的值)

You can have a keyword argument that will be a switch of whether of not you want the result printed before returning from your function: 您可以使用一个关键字参数来切换您是否希望在从函数返回之前打印结果:

def func(arg1, arg2,verbose=True):
    # do cool stuff
    # 'result' is what it all produced
    if verbose:
        print result
    return result

Then just call func(arg1,arg2,...,verbose=False) to turn it off. 然后只需调用func(arg1,arg2,...,verbose=False)将其关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM