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: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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