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


Groups > comp.lang.python > #30675

Re: local variable 'a' referenced b

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!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <demianbrecht@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.007
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; "'a'": 0.07; 'assign': 0.07; 'parameter': 0.07; "subject:' ": 0.07; 'mutable': 0.09; 'url:github': 0.09; 'def': 0.10; '#this': 0.16; 'foo()': 0.16; 'foo():': 0.16; 'subject:variable': 0.16; 'wrote:': 0.17; 'module,': 0.17; '>>>': 0.18; '2.x': 0.22; '3.2': 0.22; 'header :In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'replace': 0.27; 'skip:@ 10': 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; 'to:addr:python-list': 0.33; 'another': 0.33; 'received:google.com': 0.34; 'list': 0.35; 'pm,': 0.35; 'received:192.168.0': 0.35; 'received:209.85.220': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'message-id:@gmail.com': 0.36; 'should': 0.36; 'rather': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'header:Received:5': 0.40; 'your': 0.60; 'side': 0.61; 'ever': 0.63; 'within': 0.64; 'stuck': 0.65; 'angel': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=mTBltkbOk4vbAe/E/kBSWtvAzpYS2pWxa3ejg6JlXds=; b=wClGAdy+nzkGFsnhkqehAyLjV4Odp/Nu9qievlZFGhuh9myZrMFTTqXiBB5DXgOiTJ 9bU3Y2zIucH2Giap8ZNY/PrxvSEm/ag04wUGkI73oxG5K713KWXwVHoUd+nwepgwgkKZ /otEVYSfQ78CYxdMUYHbdqCH6VgysiBl27eG0IffnSdcSpSuoQ4CUVA5YFIcZ1iONiRf CKLFI+9lW/6HvwcsPp5YNFAn+3bCQPv09MF8EJt5vd90wexJZV+T1dSExdl6uE1sxD7G a7uaHeSRycyLSo0nwnuidlgwaCFNwhvem4vp8FP7cLFZkRjnFc3TYq/eXbODrZyPyxVh yhUg==
Date Tue, 02 Oct 2012 22:54:12 -0700
From Demian Brecht <demianbrecht@gmail.com>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20120912 Thunderbird/15.0.1
MIME-Version 1.0
To python-list@python.org
Subject Re: local variable 'a' referenced b
References <CA+YdQ_63BCEqjVd0W5vTK+r2K0K8JPdfDptFUXLc2jFESZ8Sgg@mail.gmail.com> <506BA24F.8060409@davea.name>
In-Reply-To <506BA24F.8060409@davea.name>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
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 <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1754.1349243658.27098.python-list@python.org> (permalink)
Lines 40
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1349243658 news.xs4all.nl 6844 [2001:888:2000:d::a6]:41298
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:30675

Show key headers only | View raw


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


-- 
Demian Brecht
@demianbrecht
http://demianbrecht.github.com

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: local variable 'a' referenced b Demian Brecht <demianbrecht@gmail.com> - 2012-10-02 22:54 -0700

csiph-web