从它返回的打印对象中禁用 Python return 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1040285/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 21:20:34  来源:igfitidea点击:

Disable Python return statement from printing object it returns

python

提问by Juergen

I want to disable "return" from printing any object it is returning to python shell.

我想禁止“返回”打印它返回到 python shell 的任何对象。

e.g A sample python script test.py looks like:

例如一个示例 python 脚本 test.py 看起来像:

def func1():
    list = [1,2,3,4,5]
    print list
    return list

Now, If i do following:

现在,如果我执行以下操作:

python -i test.py
>>>func1()

This always give me two prints on python shell. I just want to print and get returned object.

这总是在 python shell 上给我两个打印。我只想打印并获取返回的对象。

回答by Eli Courtwright

There's no way to configure the Python shell suppress printing the return values of function calls. However, if you assign the returned value to a variable, then it won't get printed. For example, if you say

无法配置 Python shell 以抑制打印函数调用的返回值。但是,如果将返回值分配给变量,则不会打印它。例如,如果你说

rv = func1()

instead of

代替

func1()

then nothing will get printed.

那么什么都不会打印。

回答by Juergen

The reason for the print is not the return statement, but the shell itself. The shell does print any object that is a result of an operation on the command line.

打印的原因不是return语句,而是shell本身。shell 会打印任何作为命令行操作结果的对象。

Thats the reason this happens:

这就是发生这种情况的原因:

>>> a = 'hallo'
>>> a
'hallo'

The last statement has the value of a as result (since it is not assigned to anything else). So it is printed by the shell.

最后一条语句的结果为 a(因为它没有分配给其他任何东西)。所以它是由shell打印的。

The problem is not "return" but the shell itself. But that is not a problem, since working code normaly is not executed in the interactive shell (thus, nothing is printed). Thus, no problem.

问题不在于“返回”,而在于外壳本身。但这不是问题,因为工作代码通常不会在交互式 shell 中执行(因此,不会打印任何内容)。因此,没有问题。

回答by Sean Cavanagh

In general, printing from within a function, and then returning the value, isn't a great idea.

通常,从函数内部打印然后返回值并不是一个好主意。

In fact, printing within a function is most often the wrong thing. Most often functions get called several times in a program. Sometimes you want to print the value, most often you just want to use that data for something.

事实上,在函数内打印通常是错误的。大多数情况下,函数在程序中被多次调用。有时您想打印该值,大多数情况下您只想将该数据用于某些目的。

Let the function just return it's value. Let the code that called the function print it out, if that is the right thing to do.

让函数只返回它的值。让调用该函数的代码打印出来,如果这是正确的做法。

回答by Todd

If you don't want it to show the value, then assign the result of the function to a variable:

如果您不希望它显示值,则将函数的结果分配给一个变量:

H:\test>copy con test.py
def func1():
  list = [1,2,3,4,5]
  print list
  return list
^Z
        1 file(s) copied.

H:\test>python -i test.py
>>> func1()
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
>>> x = func1()  # assign result to a variable to suppress auto printout
[1, 2, 3, 4, 5]
>>>

回答by Anh-Thi DINH

You can use something like this,

你可以使用这样的东西,

_ = my_func()