I have a very short function located within a class that keeps returning None
, even though I have turned what was previously a print statement into a return statement, here's what I have:
def explain(self):
return(print('Wear a', self.getColor(), 'shirt')
The statement will print out but every time it prints None
on the next line, please let me know how I can stop this from happening!
This is because the output of the
print
is printed on the terminal, and the value of theprint
function in itself is aNone
, which is what is returned.If you wish to return the value as well as print it, you can do something like:
If only returning the value is needed, simply remove the print statement in the above and you can print it later.
return
statement is not a function. It is a control flow construct (likeif-else
). It is what lets you "take data with you between function calls". So, you can't returnprint
, because it's not a function, it's a reserved word. But sinceprint
is a statement, you see it's output on the terminal, butNone
as well, since nothing is returned by the function. If you wish to return a string, do like this :