Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeder.news-service.com!feeder.xennanews.com!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed5.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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python': 0.08; '[0,': 0.09; 'from:addr:python': 0.09; 'def': 0.15; '"x"': 0.16; 'appends': 0.16; 'behavior?': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'reply-to:addr :python-list': 0.16; 'test()': 0.16; 'this:': 0.16; 'wrote:': 0.16; '>>>': 0.18; 'trying': 0.21; 'header:In-Reply-To:1': 0.22; 'slice': 0.23; 'variable': 0.24; 'guess': 0.26; 'string': 0.26; 'received:84': 0.28; 'constant': 0.29; 'characters,': 0.30; 'class': 0.30; 'subject:?': 0.31; 'list': 0.32; 'adds': 0.32; 'time:': 0.32; 'to:addr:python-list': 0.33; 'copying': 0.33; 'someone': 0.34; 'header:User-Agent:1': 0.34; 'creates': 0.34; 'test': 0.34; 'reply-to:addr:python.org': 0.34; 'list.': 0.35; 'subject:How': 0.35; 'explain': 0.36; 'sequence': 0.37; 'but': 0.37; 'something': 0.37; 'subject:: ': 0.39; 'itself.': 0.39; 'getting': 0.39; 'to:addr:python.org': 0.39; 'ever': 0.65; 'header :Reply-To:1': 0.71; 'reply-to:no real name:2**0': 0.71; 'so:': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=MN3iabll c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=yj_6LXTI0WAA:10 a=Wl-IwEgcsrMA:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=Zr3pj0IzLQeY5lCMfPcA:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Sun, 23 Oct 2011 01:46:55 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20110929 Thunderbird/7.0.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: How to isolate a constant? References: In-Reply-To: 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.12 Precedence: list Reply-To: python-list@python.org 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: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1319330815 news.xs4all.nl 6974 [2001:888:2000:d::a6]:47327 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:14858 On 23/10/2011 01:26, Gnarlodious wrote: > Say this: > > class tester(): > _someList = [0, 1] > def __call__(self): > someList = self._someList > someList += "X" > return someList > > test = tester() > > But guess what, every call adds to the variable that I am trying to > copy each time: > test() >> [0, 1, 'X'] > test() >> [0, 1, 'X', 'X'] > > > Can someone explain this behavior? And how to prevent a classwide > constant from ever getting changed? > '_someList' is part of the class itself. This: someList = self._someList just creates a new _reference to the list and this: someList += "X" appends the items of the sequence "X" to the list. Note that a string is also a sequence of characters, so: >>> x = [] >>> x += "XY" >>> x ['X', 'Y'] Python will copy something only when you tell it to copy. A simple way of copying a list is to slice it: someList = self._someList[:]