[Tutor] multiple function returns

Zachary Ware zachary.ware+pytut at gmail.com
Fri Jun 28 09:10:54 CEST 2013


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?
>

Essentially, return where you see fit. A lot of times, you'll see something like this:

>>> def foo(x):
...     if x:
...         return True
...     return False
... 
>>> foo(1)
True
>>> foo(0)
False


I'll leave it to you to work out why that works.  It's very handy!


Zach

----
Please excuse my phone-induced brevity.



More information about the Tutor mailing list