recursive function: use a global or pass a parameter?

Gregory Ewing greg.ewing at canterbury.ac.nz
Fri Jan 16 17:20:57 EST 2015


Tim wrote:
> I have this type of situation and wonder if I should use a global variable
> outside the recursive function instead of passing the updated parameter
> through.

No! Globals are evil, at least for that sort of thing.
The way you're doing it is fine.

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):
   ...

-- 
Greg



More information about the Python-list mailing list