Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #14858
| 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 | <python@mrabarnett.plus.com> |
| 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 <python@mrabarnett.plus.com> |
| 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 | <f5539538-d079-4be1-b2c0-d98d3fc6a33f@h39g2000prh.googlegroups.com> |
| In-Reply-To | <f5539538-d079-4be1-b2c0-d98d3fc6a33f@h39g2000prh.googlegroups.com> |
| 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 <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.2138.1319330815.27778.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
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[:]
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
How to isolate a constant? Gnarlodious <gnarlodious@gmail.com> - 2011-10-22 17:26 -0700
Re: How to isolate a constant? Chris Rebert <clp2@rebertia.com> - 2011-10-22 17:41 -0700
Re: How to isolate a constant? Gnarlodious <gnarlodious@gmail.com> - 2011-10-22 18:01 -0700
Re: How to isolate a constant? 88888 Dihedral <dihedral88888@googlemail.com> - 2011-10-22 22:12 -0700
Re: How to isolate a constant? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-10-23 05:32 +0000
Re: How to isolate a constant? Paul Rudin <paul.nospam@rudin.co.uk> - 2011-10-23 11:23 +0100
Re: How to isolate a constant? MRAB <python@mrabarnett.plus.com> - 2011-10-23 01:46 +0100
Re: How to isolate a constant? Alan Meyer <ameyer2@yahoo.com> - 2011-10-25 15:50 -0400
Re: How to isolate a constant? Ian Kelly <ian.g.kelly@gmail.com> - 2011-10-25 14:05 -0600
Re: How to isolate a constant? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2011-10-25 17:08 -0700
Re: How to isolate a constant? Mel <mwilson@the-wire.com> - 2011-10-25 22:48 -0400
Re: How to isolate a constant? Ian Kelly <ian.g.kelly@gmail.com> - 2011-10-25 18:30 -0600
Re: How to isolate a constant? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2011-10-22 17:55 -0700
csiph-web