Is it bad style to return a print statement?

Because I initially learned about printing to the screen, and return was used for supply data or other outputs from the function, I never considered returning a print statement:

def method():
   # stuff
    return print(“some string”)

This is no different that returning any other method and argument, but I don’t see it very often in code. Is this because people will normally just print and not bother about returning the function. Or is it just a bad approach even though it satisfies both the need to print to screen and supply a value (other than None) which can then we use further (for example, assertions)?

Every function returns something.
If you just do print(“some string”), that function will return None and it will print “some string”
return() returns a VALUE, not another function.
print() is just a function that executes a command that ceases on the next line.
You can read more about it here:
http://interactivepython.org/runestone/static/pip2/Functions/Printvs.return.html

Not every function returns a value, that is why ‘None’ is sometimes returned (like print(), exit()). Okay, that is maybe semantics but you know what I mean.

That article actually answers my query

If your only purpose in running a function is to make an output visible for human consumption…The other possibility is to return a value from the function and print it, as in print f(3). As you start to write larger, more complex programs, this will be more typical. Indeed the print statement will usually only be a temporary measure while you’re developing the program. Eventually, you’ll end up calling f and saving the return value or using it as part of a more complex expression.

This is what i was trying to explain. You rarely see the value of the print statement returned, although it works. It becomes important if you want to check what is being printed.

But, and it’s a big but…checking what is being printed ‘for human consumption’ is a bit of a mixed up idea. I can see in a more complex program you would return a value and deal with that elsewhere (like maybe adding it to a dict and calling print on that dict as a separate function).

This is quite simple when you think about it, but much of LPTHW is based on text printed ‘for human consumption’. Its only when you get into testing it that you recognise the futility.

1 Like

Yeah, I meant something, not necessarily a value. It’s either a value or None.
Yes, print() is only for humans.

Yes, I wouldn’t do that. It’s basically harmless but weird. So, languages break down into statement based or expression based (mostly). An expression based language does follow the rule that everything returns something, even if that something is None. Ruby is like this if IIRC so I think you can do things like return an if-else result by just return in front of the whole if.

Python however is statement based, which means you can’t do stuff like returning an if. Now, print used to be a statement, but it changed in Python 3 so now this should work, but it kind of doesn’t mean anything.

2 Likes