recursive function: use a global or pass a parameter?

Chris Angelico rosuav at gmail.com
Fri Jan 16 18:49:41 EST 2015


On Sat, Jan 17, 2015 at 9:20 AM, Gregory Ewing
<greg.ewing at canterbury.ac.nz> wrote:
> The only thing I would change is to wrap it all up
> in a top-level function that takes care of creating
> the result set and returning it.
>
> def walk(obj):
>   res = set()
>   _walk(obj, res)
>   return res
>
> def _walk(obj, res):
>   ...

Point of style: I like to put these kinds of helpers _above_ the
corresponding public functions, to maintain a general policy of
Define-Before-Use. Tends to make code easier to read; the first
reference to a function name is its definition, then all usage comes
after that. It's not always possible, of course (eg mutual recursion),
but in simple cases like this, it's easy enough.

ChrisA



More information about the Python-list mailing list