Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #83896

Re: recursive function: use a global or pass a parameter?

From Peter Otten <__peter__@web.de>
Subject Re: recursive function: use a global or pass a parameter?
Date 2015-01-16 19:34 +0100
Organization None
References <5e4ccec6-7a00-467d-8cf6-258ab0421c90@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.17804.1421433277.18130.python-list@python.org> (permalink)

Show all headers | View raw


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.
> 
> I want to get a union of all the values that any 'things' key may have,
> even in a nested dictionary (and I do not know beforehand how deep the
> nesting might go):
> 
> d = {'things':1, 'two':{'things':2}}
> 
> def walk(obj, res):
>     if not hasattr(obj, 'keys'):
>         return set(), set()
>     
>     if 'things' in obj:
>         res.add(obj['things'])
>         
>     for k in obj:
>         walk(obj[k], res)
>         
>     return res
> 
> walk(d, set()) # returns {1, 2}
> 
> Is it better to use a global to keep track of the values or does it even
> matter?

Globals are generally bad as they make code non-reentrant; when two calls of 
the function run simultaneously the data will be messed up.

I recommend that you use a generator:

>>> def walk(obj):
...     if not hasattr(obj, "keys"):
...         return
...     if "things" in obj:
...         yield obj["things"]
...     for v in obj.values():
...         yield from walk(v)
... 
>>> d = {'things':1, 'two':{'things':2}}
>>> set(walk(d))
{1, 2}

In Python before 3.3 you have to replace

yield from walk(v)

with a loop:

for t in walk(v):
    yield t

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

recursive function: use a global or pass a parameter? Tim <jtim.arnold@gmail.com> - 2015-01-16 09:49 -0800
  Re: recursive function: use a global or pass a parameter? Chris Angelico <rosuav@gmail.com> - 2015-01-17 04:56 +1100
    Re: recursive function: use a global or pass a parameter? Rustom Mody <rustompmody@gmail.com> - 2015-01-16 10:22 -0800
  Re: recursive function: use a global or pass a parameter? Peter Otten <__peter__@web.de> - 2015-01-16 19:34 +0100
    Re: recursive function: use a global or pass a parameter? Tim <jtim.arnold@gmail.com> - 2015-01-16 10:48 -0800
    Re: recursive function: use a global or pass a parameter? Yawar Amin <yawar.amin@gmail.com> - 2015-01-16 18:23 -0800
      Re: recursive function: use a global or pass a parameter? Yawar Amin <yawar.amin@gmail.com> - 2015-01-16 21:29 -0800
      Re: recursive function: use a global or pass a parameter? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2015-01-18 10:25 +1300
  Re: recursive function: use a global or pass a parameter? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2015-01-17 11:20 +1300
    Re: recursive function: use a global or pass a parameter? Chris Angelico <rosuav@gmail.com> - 2015-01-17 10:49 +1100
  Re: recursive function: use a global or pass a parameter? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-01-17 21:15 +1100
    Re: recursive function: use a global or pass a parameter? Roy Smith <roy@panix.com> - 2015-01-17 10:20 -0500
      Re: recursive function: use a global or pass a parameter? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2015-01-18 10:07 +1300
  Re: recursive function: use a global or pass a parameter? albert@spenarnc.xs4all.nl (Albert van der Horst) - 2015-01-17 17:30 +0000
    Re: recursive function: use a global or pass a parameter? Chris Angelico <rosuav@gmail.com> - 2015-01-18 06:27 +1100

csiph-web