Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.85.MISMATCH!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; "'a'": 0.07; 'assign': 0.07; 'mask': 0.07; 'nested': 0.07; 'parameter': 0.07; "subject:' ": 0.07; 'assumed': 0.09; 'mutable': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; '#this': 0.16; 'closure,': 0.16; 'foo()': 0.16; 'foo():': 0.16; 'subject:variable': 0.16; 'wrote:': 0.17; 'module,': 0.17; 'examples': 0.18; '>>>': 0.18; 'variable': 0.20; '2.x': 0.22; '3.2': 0.22; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'replace': 0.27; "doesn't": 0.28; 'documenting': 0.29; '(including': 0.30; 'code': 0.31; 'print': 0.32; '(a)': 0.33; "aren't": 0.33; 'problem': 0.33; 'another': 0.33; 'list': 0.35; 'otherwise.': 0.35; 'pm,': 0.35; 'really': 0.36; 'but': 0.36; 'should': 0.36; 'rather': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'received:192': 0.39; 'short': 0.39; 'received:192.168': 0.40; 'your': 0.60; 'side': 0.61; 'ever': 0.63; 'within': 0.64; 'stuck': 0.65; 'talking': 0.66; 'header:Reply-To:1': 0.68; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; 'sharing': 0.74; 'received:74.208.4.194': 0.84; 'seldom': 0.84; 'angel': 0.93 Date: Wed, 03 Oct 2012 02:08:41 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0 MIME-Version: 1.0 To: Demian Brecht Subject: Re: local variable 'a' referenced b References: <506BA24F.8060409@davea.name> <506BD304.6090905@gmail.com> In-Reply-To: <506BD304.6090905@gmail.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:HNWGa3bRhFWERix2DIuAf3kYRwdKlbJeLLuPvFa64OZ QloJww5mWUQZ+gv4XRobhRHaSws35xR4iLkZyZo3wN9MbAionI qdqLzxrZk5KQKKr6pl8XNlghlPoM9mVxMvrCUtlHtAEOlLH8dj dmGkV6IsHE6N4HgpzxV3c8gv76TZ5JjMuh+cVneXQtskG09ipr 4hPYrt/WGwaTpW1mzbcFtThyk0QzI4GPyrARMCRgCG+lhrfyyK YO2SFrWk+ZwL76EUXqfoWyT2l1rbtcVQIWr6QImcqfSfDzP/46 Ce7KT9FjXZ3m2ZFv9xZj0SVrcWvtDYSYBKyUGG82dTrMboW7A= = Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: d@davea.name 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: 48 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1349244548 news.xs4all.nl 6930 [2001:888:2000:d::a6]:45891 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:30676 On 10/03/2012 01:54 AM, Demian Brecht wrote: > On 12-10-02 07:26 PM, Dave Angel wrote: >> >> if you're stuck with Python2.x, you can use a mutable object for a, and >> mutate it, rather than replace it. For example, >> >> >> def foo(): >> a = [3] >> def bar(): >> b=2 >> a.append(b) #this mutates a, but doesn't assign it >> print (a) >> a[0] += b #likewise, for a number within the list >> print (a) >> bar() >> >> That should work in either 2.x or 3.2 >> > > Alternatively, you can restructure your code by simply adding a > parameter to bar(). Nice thing about this is that if you ever move > bar() out into another module, then you don't have to worry about > documenting the side effects on 'a' so users (including yourself) > aren't confused later: > > >>> def foo(): > ... a = 1 > ... def bar(n): > ... b = 2 > ... return n + b > ... a = bar(a) > ... print a > ... > >>> foo() > 3 > > One problem with short examples is they mask the reason for the code to be structured that way. I assumed that the OP was really talking about a closure, and that sharing that variable was deliberate. I seldom write nested functions otherwise. -- DaveA