"return" in def

Roger rdcollum at gmail.com
Sun Dec 28 21:06:25 CET 2008

> Curious. When I see a bare return, the first thing I think is that the
> author forgot to include the return value and that it's a bug.
> The second thing I think is that maybe the function is a generator, and
> so I look for a yield. If I don't see a yield, I go back to thinking
> they've left out the return value, and have to spend time trying to
> understand the function in order to determine whether that is the case or
> not.
> In other words, even though it is perfectly valid Python, bare returns
> always make the intent of the function less clear for me. I'm with Bruno
> -- if you have a function with early exits, and you need to make the
> intent of the function clear, explicitly return None. Otherwise, leave it
> out altogether.
> --
> Steven

To me this is the soundest argument.  Thanks for the advice.  I think
I'll follow this as a rule of thumb hereafter.

More information about the Python-list mailing list