Function passed as an argument returns none

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Oct 2 09:59:50 CEST 2014


Chris Angelico wrote:

> On Thu, Oct 2, 2014 at 12:29 PM, Rustom Mody <rustompmody at gmail.com>
> wrote:
>> So by now you know there are 2 kinds of return:
>>
>> So the morals in short:
>>
>> 1. Stick to the return that works -- python's return statement --
>> and avoid the return that seems to work -- the print statement
> 
> Please. There are not two types of return; there are two completely
> different things here. Don't pretend that print is a bad form of
> return. It isn't.

I strongly agree with Chris here. The OP's problem was due to confusion
between print and return, and the solution is to learn the difference
between printing output to the screen and returning values from a function,
and under what circumstances Python will automatically print said returned
values as a convenience. Conflating the two as "2 kinds of return" is an
invitation to even more confusion: "which was the 'good' return again?".

While it is true that both return and print are related in the sense that
they are both ways of having a function produce output, that's about the
end of the similarity. One might as well say that assignment is a third
kind of return, because one can assign output to a global variable.

(Since print does not actually exit the current sub-routine, it definitively
is not a kind of return.)


-- 
Steven




More information about the Python-list mailing list