Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #83896
| Path | csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'nested': 0.07; 'parameter': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'def': 0.12; 'globals': 0.16; 'hasattr(obj,': 0.16; 'nesting': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'res': 0.16; 'set()': 0.16; 'subject:pass': 0.16; 'through.': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'passing': 0.19; '>>>': 0.22; 'header:User- Agent:1': 0.23; 'replace': 0.24; 'values': 0.27; 'header:X -Complaints-To:1': 0.27; 'function': 0.29; 'generally': 0.29; 'tim': 0.29; 'wonder': 0.29; 'have,': 0.30; 'code': 0.31; "skip:' 10": 0.31; 'run': 0.32; 'up.': 0.33; 'updated': 0.34; 'yield': 0.36; 'subject:?': 0.36; 'should': 0.36; 'two': 0.37; 'to:addr :python-list': 0.38; 'track': 0.38; 'does': 0.39; 'bad': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'even': 0.60; 'skip:n 10': 0.64; 'situation': 0.65; 'union': 0.69; 'messed': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: recursive function: use a global or pass a parameter? |
| Date | Fri, 16 Jan 2015 19:34:23 +0100 |
| Organization | None |
| References | <5e4ccec6-7a00-467d-8cf6-258ab0421c90@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p57bd8b85.dip0.t-ipconnect.de |
| User-Agent | KNode/4.13.3 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.17804.1421433277.18130.python-list@python.org> (permalink) |
| Lines | 55 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1421433277 news.xs4all.nl 2973 [2001:888:2000:d::a6]:57008 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:83896 |
Show key headers only | 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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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