1

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.

Miguel
  • 7,497
  • 2
  • 27
  • 46
  • Don't use the function in a Python interactive session then. It echoes the results of an expression, which is why you see the `4` being printed. You could use `temp = func(2)` to prevent this. – Martijn Pieters Oct 01 '15 at 16:18
  • 1
    I never saw so many downvotes so quick. What is so obviously wrong with this question (honestly, I would like to know)? – Miguel Oct 01 '15 at 16:24
  • Well, a minimal example would be to type `2` to the python console. The problem has nothing to do with printing in a function or even just calling it. – Karoly Horvath Oct 01 '15 at 16:30
  • @KarolyHorvath Then I'm being penalized for not knowing that? How is this site supposed to work if you're penalized for not knowing the answer to your questions. Obviously if I knew this behavior cannot be prevented I would not have asked the question. – Miguel Oct 01 '15 at 16:32
  • The downvoters probably feel you didn't do enough research before posting, but I feel your question just reflects a misunderstanding about how the interactive interpreter works. That misunderstanding would make it hard to even know what the real problem is, let alone how to fix it. – chepner Oct 01 '15 at 16:32
  • @Miguel: I'm just saying you haven't provided a *minimal* example. Do you really want to argue with that? – Karoly Horvath Oct 01 '15 at 16:37
  • @KarolyHorvath But this is a minimal example of my problem. My actual problem is a numerical minimization where the function prints to screen the results of the calculation but I also want those results to be available as a list that the user can use for further processing. – Miguel Oct 01 '15 at 16:40
  • 1
    I have no idea why this question is downvoted so much. This is the exact same question I wanted to ask just now, so I'm giving it my upvote. Kudos for not deleting the question after those 5 quick downvotes. – Joeytje50 Oct 21 '15 at 19:20
  • @Joeytje50 Thanks for the moral support :-D – Miguel Oct 21 '15 at 19:37

1 Answers1

4

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.

Community
  • 1
  • 1
enigma
  • 3,476
  • 2
  • 17
  • 30
  • I am expecting someone to use the code. How should I be running it instead? – Miguel Oct 01 '15 at 16:23
  • @Miguel by writing the code and saving it in a `.py` file. They can then run it from a terminal / command line, or any other means applicable. – enigma Oct 01 '15 at 16:27
  • But I want people to be able to both use it interactively and from a script. The function is already in a `.py` file and the usage is as imported module. – Miguel Oct 01 '15 at 16:28
  • 1
    @Miguel if you want people to be able to use it in the interactive interpreter, then you'll probably have to cater your return value with the behaviour in mind. I can't quite think of an example where the return value does not hold information useful to the user in this particular manner, but I conceed they probably exist. In all honesty, I'm not sure why you're getting quite such an extreme response here, it seems a little unfair to a genuine question. – enigma Oct 01 '15 at 16:35
  • @enigma I just illustrated the issue a bit better. – Miguel Oct 01 '15 at 16:49
  • @Miguel I've added a suggestion, but it quite possibly isn't ideal for your situation. More of a work around really. – enigma Oct 01 '15 at 17:00
  • @enigma Thanks, I think I might be able to combine your suggestions into something I can live with. – Miguel Oct 01 '15 at 17:23