Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

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. 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

From the accepted answer there:

__main__.__file__ doesn't exist in the interactive interpreter:

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

This also goes for code run via python -c, but not 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...