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


Groups > comp.lang.python > #83903

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

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 <rosuav@gmail.com>
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 <chth6bF4cv1U1@mid.individual.net>
References <5e4ccec6-7a00-467d-8cf6-258ab0421c90@googlegroups.com> <chth6bF4cv1U1@mid.individual.net>
Date Sat, 17 Jan 2015 10:49:41 +1100
Subject Re: recursive function: use a global or pass a parameter?
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <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 <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.17808.1421452183.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Sat, Jan 17, 2015 at 9:20 AM, Gregory Ewing
<greg.ewing@canterbury.ac.nz> 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

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