Reputation: 221
I'm writing a Python script, and it needs to print a line to console. Simple enough, but I can't remember (or decide) whether the accepted practice is to print in the function, for example:
def exclaim(a):
print (a + '!')
or have it return a string, like:
def exclaim(a):
return (a + '!')
and then print in the main script. I can make an argument for either method. Is there a generally accepted way to do this or is it up to preference?
EDIT: To clarify, this isn't the function I'm working with. I don't feel comfortable posting the code here, so I wrote those functions as an oversimplified example.
Upvotes: 5
Views: 3673
Reputation: 22443
If a function computes values and then prints them immediately, I regularly find that I will eventually want to display or use the values in a different place or in a different way.
I'll decide to write the results to a file, send them out as part of a web page, use them as a partial value for other functions, display them sorted or filtered differently, etc.
It's fine to have functions print computed values immediately when you're prototyping, or if the functions are very simple...but sooner or later, if a program grows into something larger and more valuable, you'll need to convert that simple "just print it!" function into a purer compute-and-return function.
So I try to just start with a return-the-results, print-them-later coding style. It seems cleaner and generally saves me time later. But as with all things stylistic, it's a judgment call, and YMMV.
Upvotes: 0
Reputation: 308158
One of the things I most appreciate about Python is that it encourages proper separation of concerns. There's so little friction in splitting a task down to its atomic components.
Calculating a value and displaying a value are two different things, they should not be mixed together.
Upvotes: 2
Reputation: 27575
I mix both ways all the time, but I think it could boil down like this:
If the sole purpose of the whole function is output to terminal (print a message to the user), then the function should print. Optionally, you could use some return value as an error status or boolean value, but as a side step. Usually the printed string/message would not have other uses inside the program flow, because the user received the message and the string variable, having been consumed, can be garbage-collected.
If the function actually PERFORMS something, thus returning a real value (even if this value is a string), and you'd like to check that value on the terminal, than you call the function and print its result in another statement. In case of debugging, you just remove or comment that print statement, but the function will continue to perform silently. Just for comparison, if the print statement were inside the function, you'd need to modify the function itself.
As a last consideration, both approaches are somewhat equivalent, but for anyone reading your code (even yourself some time later), the way it is layed out aids to capture the INTENT of the code, in a readability point of view.
Hope this helps
Upvotes: 0
Reputation: 46728
If you are sure that function could not be re-used, and you formed that function just to enhance code readablilty, you could go with printing it within the function.
Else, if there is any way you can re-use it in any way later, you should return the value instead. The second approach is preferred, as one of the purposes of a function is re-use.
There is no real overhead.
Upvotes: 1
Reputation: 1170
One thing to keep in mind is if you print it your function will return nothing, so if you ever plan on using the line your wish to write you should return it.
Upvotes: 1
Reputation: 838186
Normally a function should generate and return its output and let the caller decide what to do with it.
If you print the result directly in the function then it's difficult to reuse the function to do something else, and it's difficult to test as you have to intercept stdout rather than just testing the return value.
However in this trivial example it hardly seems necessary to either reuse or test the function, or even to have the function at all.
Upvotes: 6