简体   繁体   English

防止 Python 打印返回结果

[英]Prevent Python from printing return result

I have a function that both returns a value and does some printing.我有一个函数,它既返回一个值又进行一些打印。 Schematically:示意图:

def func(x):
 print "You're using this function"
 return x**2

There are two different ways in which I might want to use the function, one is to do the printing so that I can read the info, but another one is to both print and store the result in a variable:我可能想以两种不同的方式使用该函数,一种是进行打印以便我可以读取信息,但另一种是打印并将结果存储在变量中:

>>> func(2)
You're using this function
4
>>> a=func(2)
You're using this function

I would like to prevent the first usage from printing 4 .我想防止第一次使用打印4 Can I do this modifying the function definition somehow?我可以以某种方式修改函数定义吗?


Edit编辑

Since people seem to have some trouble understanding where I'm coming from, this is my function's output when I use it interactively:由于人们似乎难以理解我来自哪里,这是我交互使用函数时的输出:

>>> ela_dist(c_voigt, rotate = True)

************************** R E S U L T S **************************
Results with rotation optimization                                 

Symmetry     Euclidean distance     Angles tx,     ty,     tz      
--------     ------------------     -------------------------------
     iso             139.65 GPa           n/a     n/a     n/a  deg.
     cub              83.66 GPa         -1.89   -1.83    6.37  deg.
     hex             110.23 GPa         -2.14   -4.22     n/a  deg.
       3             102.16 GPa         -3.27   -6.94     n/a  deg.
      32             102.16 GPa         -3.27   -6.94     n/a  deg.
       4              82.32 GPa         -2.52   -1.15    2.14  deg.
     4mm              82.32 GPa         -2.52   -1.15    6.31  deg.
     ort              78.00 GPa         -3.05   -1.71    7.87  deg.
     mon              62.62 GPa         -2.85    0.76    7.62  deg.
************************** R E S U L T S **************************

[['iso', 139.65433517558037, 0.0, 0.0, 0.0], ['cub', 83.663957459095329, -1.8878916427147878, -1.8303975226851734, 6.3671511063645525], ['hex', 110.23296814646618, -2.1378994103876803, -4.2246840445795071, 0.20038249224556773], ['3', 102.16396472738577, -3.2709875018178636, -6.9354445747734825, 5.1898595103729814], ['32', 102.16396472738596, -3.2709866369782259, -6.9354442259896656, -20.03283399363794], ['4', 82.321990809120038, -2.5218897967148739, -1.1525909395834488, 2.1400405876841635], ['4mm', 82.32199080912001, -2.5218897552576087, -1.1525909978835307, 6.3069829557712076], ['ort', 78.001968057183262, -3.0530030281994716, -1.7132006819033545, 7.8685738019737688], ['mon', 62.623413013297196, -2.8503971823612497, 0.75895564714111607, 7.6204664688915926]]

So I am generating a "nice" output and then there is this long list with results that I do not want to show.所以我正在生成一个“不错”的输出,然后有一个我不想显示的长列表。 However, I want the user to have to possibility to store the results in a variable for later use.但是,我希望用户能够将结果存储在变量中以备后用。

Because you're running the code in the interactive interpreter, the return value will always be printed out.因为您在交互式解释器中运行代码,所以将始终打印出返回值。 If you run the code in a script, the return value will not be printed.如果在脚本中运行代码,则不会打印返回值。

In general there's not really a downside to having the return value printed, it is extremely useful for debugging.一般来说,打印返回值并没有真正的缺点,它对调试非常有用。 If you are expecting someone else to use your code, you shouldn't be running it in this manner anyway.如果您希望其他人使用您的代码,那么无论如何您都不应该以这种方式运行它。


After your further edit, you might want to tailer the behaviour of your function based on whether or not it's being run in the interactive interpreter.进一步编辑后,您可能希望根据函数是否在交互式解释器中运行来调整函数的行为。 See this question for further details: Tell if Python is in interactive mode有关更多详细信息,请参阅此问题: 判断 Python 是否处于交互模式

From the accepted answer there:从那里接受的答案:

__main__.__file__ doesn't exist in the interactive interpreter: __main__.__file__在交互式解释器中不存在:

 import __main__ as main print hasattr(main, '__file__')

This also goes for code run via python -c , but not python -m .这也适用于通过python -c运行的代码,但不适用于python -m

You could decide not to return the values you are doing for instance.例如,您可以决定不返回您正在执行的值。 However, this doesn't really solve your problem.但是,这并不能真正解决您的问题。

What I would advise doing, although you could argue it'd make your program a little more clunky for the user, is define a display function, that would be called if your user wanted the pretty result table.我建议做的事情,虽然你可能会争辩说它会让你的程序对用户来说更笨拙,但是定义一个显示函数,如果你的用户想要漂亮的结果表,就会调用它。 Usage would then be用法将是

>>> display(ela_dist(c_voigt, rotate = True))

or something of the sort.或类似的东西。

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

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