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: 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 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: 1421522845 news.xs4all.nl 2973 [2001:888:2000:d::a6]:58313 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:83945 On Sun, Jan 18, 2015 at 4:30 AM, Albert van der Horst 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