"return" in def
John Machin
sjmachin at lexicon.net
Sun Dec 28 17:12:11 EST 2008
On Dec 29, 7:06 am, Roger <rdcol... at gmail.com> wrote:
> > 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.
Please don't. Follow MRAB's advice, with the corollary that a
generator is forced by the compiler to be a "procedure" in MRAB's
terminology.
More information about the Python-list
mailing list