[Tutor] multiple function returns

Jerry Hill malaclypse2 at gmail.com
Fri Jun 28 21:50:39 CEST 2013


On Fri, Jun 28, 2013 at 12:18 AM, Jim Mooney <cybervigilante at gmail.com> wrote:
> What's the Pythonic standard on multiple returns from a function? It
> seems easiest to just return from the point where the function fails
> or succeeds, even it that's multiple points. Or is it considered best
> to defer everything to one return at the end?

My personal preference depends on how big the function is.  If the
function is short enough to fit on one screen or so in my editor, then
I just go ahead and return wherever it makes sense to do so -- in
multiple places if that makes sense for the particular function.  On
the other hand, if the function is longer than that, I prefer to
figure out my result along the way, then do a single return at the end
of the function.

For me, this practice helps to increase the maintainability of my
code.  Any function short enough to fit on one screen is usually short
enough to fit in my mind all at once too, so the multiple returns
don't get confusing.  For longer functions, I can't always take in how
the whole thing works in a single go, and having the single exit point
at the end seems to help me when I trace through the code.

Again, this is a personal preference.  I know some teams prefer to
have everything in either one style or the other for the sake of
consistency.  Experience is the only way for you to figure out what
works best with your debugging style.

--
Jerry


More information about the Tutor mailing list