Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #83945
| Path | csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!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.005 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'parameters': 0.04; 'argument': 0.05; 'nested': 0.07; 'think,': 0.07; 'default.': 0.09; 'useless': 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; 'materially': 0.16; 'subject:pass': 0.16; 'subroutine': 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; 'all:': 0.24; 'helper': 0.24; 'fairly': 0.24; 'cc:2**0': 0.24; 'van': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'could': 0.34; 'received:google.com': 0.35; 'subject:?': 0.36; 'effort': 0.37; 'example,': 0.37; 'e.g.': 0.38; 'does': 0.39; 'itself': 0.39; 'even': 0.60; 'simple': 0.61; 'different': 0.65; '2015': 0.84; 'albert': 0.91; 'fibonacci': 0.91; 'to:none': 0.92; 'technique': 0.93 |
| 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=ESlGGR7GFGWvT85fWcyULbBzt/mw9tGOVI122mqMyVo=; b=KQQE5Zw4BEDvxvwttM7S51FEbQl6XM7ENxSe293A5kHJqFOQEsGs/nUSP/rNqJr/gG nNY/dUvEi0BCSBVVuEME7LUUEVJNexoOkZL39XAlhvcWeieZ86raphHQ3vxQeVDAKvxK AZ4/OHdoHSgnQwKLHzqOYnJV9CbZv6qeFhPpG8TBkuwXiHLxc7C6nDPGdXTzAO0eKibf S2QTiBeoFqXKKZyFAdWq4D4xpRT8QK696PqTfWruRGXd07HQcOxA4pxIvcEsyj6QvZzP ClsYtU1pvfDBsVu36MKVInCiKcIc2O7fkzM/rCLWyhguXtM00N7plWCKuIH2MY5u2oxL Becw== |
| MIME-Version | 1.0 |
| X-Received | by 10.50.79.196 with SMTP id l4mr10506492igx.14.1421522843043; Sat, 17 Jan 2015 11:27:23 -0800 (PST) |
| In-Reply-To | <54ba9c1c$0$15938$e4fe514c@dreader35.news.xs4all.nl> |
| References | <5e4ccec6-7a00-467d-8cf6-258ab0421c90@googlegroups.com> <54ba9c1c$0$15938$e4fe514c@dreader35.news.xs4all.nl> |
| Date | Sun, 18 Jan 2015 06:27:22 +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.17817.1421522845.18130.python-list@python.org> (permalink) |
| Lines | 33 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1421522845 news.xs4all.nl 2973 [2001:888:2000:d::a6]:58313 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:83945 |
Show key headers only | View raw
On Sun, Jan 18, 2015 at 4:30 AM, Albert van der Horst
<albert@spenarnc.xs4all.nl> wrote:
> The proper technique is make the global local to the normal subroutine,
> then make the subroutine with those parameters you don't want to see
> also local to that subroutine.
> E.g.
>
> def fib(n):
> ' return the n-th Fibonacci number '
> a,b = 0,1
> def fib1(ap,bp):
> ' for f_n,f_n+1, return f_n+1,f_n+2 '
> return bp,ap+b
> for i in xrange(n):
> a,b = fib1(a,b)
> return a
That's a fairly useless use of a nested function... you could in-line
it without any effort at all:
def fib(n):
a,b = 0,1
for i in xrange(n):
a,b = b,a+b
return a
Even in the OP's example, where the inner function needs to call
itself recursively, it doesn't need to be a closure; a simple
out-of-line helper function does work (and has already been suggested;
Gregory was, I think, first). In my opinion, it's not materially
different from having an argument with a default.
ChrisA
Back to comp.lang.python | Previous | Next — Previous 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