Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1a.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.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'definition,': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'jan': 0.12; '9:20': 0.16; 'enough.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'helpers': 0.16; 'res': 0.16; 'set()': 0.16; 'subject:pass': 0.16; 'tends': 0.16; 'top-level': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'possible,': 0.19; 'cc:addr:python.org': 0.22; 'creating': 0.23; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'am,': 0.29; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; 'easier': 0.31; 'that.': 0.31; 'cases': 0.33; 'but': 0.35; 'received:google.com': 0.35; 'returning': 0.36; 'subject:?': 0.36; 'easy': 0.60; 'course': 0.61; 'simple': 0.61; 'first': 0.61; 'name': 0.63; '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=X4AWrysKZe+4W/S7HMWOmpU7IoF2hniRr8djEmNtIcc=; b=csF7SVXqKNUB4gFxq/2XnlYcUxi8LSTd0PjgHMj1X/rkIdjjpiXsw6OLzyZu70xgRT ZCHUIjVsErIo4LiImyOrlX8QoeMvW357xFiW01m8L7icL4GMYCl1AWqKStpKz/UChopE Exy2nOPLlMqVNmcY/m3OEjkrLixyMAXUQ37tXrRMTdNXJul/99jNvm/xAyHPAQiOee+8 sqnHe25/Z3dlC+4J2lXw5VndhQa/lURyPEbzebgprLQTcXLOXxn9JDG4si/BRVNWuA/H JktJMwyyla5XwC3YOua3A9U0tb3iOkiUxQ6fxYCPPANdbW5mucbDkHYmmkcooJjQBl4F RirQ== MIME-Version: 1.0 X-Received: by 10.107.7.94 with SMTP id 91mr19045308ioh.27.1421452181327; Fri, 16 Jan 2015 15:49:41 -0800 (PST) In-Reply-To: References: <5e4ccec6-7a00-467d-8cf6-258ab0421c90@googlegroups.com> Date: Sat, 17 Jan 2015 10:49:41 +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: 22 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1421452183 news.xs4all.nl 2891 [2001:888:2000:d::a6]:53361 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:83903 On Sat, Jan 17, 2015 at 9:20 AM, Gregory Ewing wrote: > 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): > ... Point of style: I like to put these kinds of helpers _above_ the corresponding public functions, to maintain a general policy of Define-Before-Use. Tends to make code easier to read; the first reference to a function name is its definition, then all usage comes after that. It's not always possible, of course (eg mutual recursion), but in simple cases like this, it's easy enough. ChrisA