Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'nested': 0.07; 'none:': 0.07; 'global,': 0.09; 'parameter': 0.09; 'val': 0.09; 'cc:addr :python-list': 0.11; 'def': 0.12; 'jan': 0.12; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'hasattr(obj,': 0.16; 'nesting': 0.16; 'res': 0.16; 'set()': 0.16; 'subject:pass': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'tim': 0.29; 'have,': 0.30; 'message- id:@mail.gmail.com': 0.30; "skip:' 10": 0.31; "i'd": 0.34; 'but': 0.35; 'received:google.com': 0.35; 'subject:?': 0.36; 'track': 0.38; 'rather': 0.38; 'does': 0.39; 'how': 0.40; 'even': 0.60; 'union': 0.69; '2015': 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=F6N9/IUw6xdrtMc4gqZni9/I1l5UhiqBZQMD2v20MKk=; b=qGEyf1T7FsSW4BqaPgf9QSbtwqDqiDPvcYi0GHfc5j73bqq1cM1Yd7dX6JRodjXAnh 8g2LMyXRuq6Bh1YRNAqvobXh5X3z07Zo/jwPgKwgV4MQTUS+jV+1Bs+pTHV7kh96Kf+R 2DGs1Hi712/i9nvhDS4POcwKmN3IQW949Eik12c8AuKG7VUr8KQkVcxdwwQwdeR5+iUg E+dtUthSwW67d4ESJxlKwQnVGk2Au4u/f2JFETyEdH6Jjfmz/qsxtk44i7lWXMZwfJZ1 HaC2jzTtIBrh/MIPHYgJYuuLfi69nIZb0zs2+Jd3nAd/xamCCUQEvm7F0xAZwn58freu zR+g== MIME-Version: 1.0 X-Received: by 10.42.52.200 with SMTP id k8mr8330394icg.26.1421430968996; Fri, 16 Jan 2015 09:56:08 -0800 (PST) In-Reply-To: <5e4ccec6-7a00-467d-8cf6-258ab0421c90@googlegroups.com> References: <5e4ccec6-7a00-467d-8cf6-258ab0421c90@googlegroups.com> Date: Sat, 17 Jan 2015 04:56:08 +1100 Subject: Re: recursive function: use a global or pass a parameter? From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 33 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1421430971 news.xs4all.nl 2887 [2001:888:2000:d::a6]:41734 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:83891 On Sat, Jan 17, 2015 at 4:49 AM, Tim wrote: > 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? I would use a parameter rather than a global, but I'd make the parameter optional: def all_keys(obj, accum=None): if accum is None: accum=set() if 'things' in obj: res.add(obj['things']) for val in obj.values(): all_keys(val, accum) return all_keys ChrisA