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


Groups > comp.lang.python > #14859

Re: How to isolate a constant?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'oct': 0.02; 'python': 0.08; '[0,': 0.09; 'mutable': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'def': 0.15; '"x"': 0.16; '[:]': 0.16; 'behavior?': 0.16; 'bieber': 0.16; 'bound,': 0.16; 'declaimed': 0.16; 'email addr:ix.netcom.com': 0.16; 'email name:wlfraed': 0.16; 'from:addr:ix.netcom.com': 0.16; 'from:addr:wlfraed': 0.16; 'from:name:dennis lee bieber': 0.16; 'it...': 0.16; 'received:66.245': 0.16; 'received:dsl.mindspring.com': 0.16; 'received:mindspring.com': 0.16; 'received:wlfraed': 0.16; 'retrieves': 0.16; 'sublist': 0.16; 'url:netcom': 0.16; 'url:wlfraed': 0.16; 'wulfraed': 0.16; 'this:': 0.16; 'trying': 0.21; 'url:home': 0.21; '-0700': 0.23; 'variable': 0.24; 'guess': 0.26; 'lee': 0.28; 'sat,': 0.28; 'constant': 0.29; 'temporary': 0.29; 'second': 0.29; 'exists,': 0.30; 'object.': 0.30; 'class': 0.30; 'subject:?': 0.31; 'list': 0.32; 'adds': 0.32; 'time:': 0.32; "can't": 0.33; 'to:addr:python- list': 0.33; 'done': 0.34; 'someone': 0.34; 'creates': 0.34; 'test': 0.34; 'copied': 0.34; 'header:X-Complaints-To:1': 0.35; 'reference': 0.35; 'object': 0.35; 'subject:How': 0.35; 'charset :us-ascii': 0.36; 'explain': 0.36; 'created': 0.36; 'skip:" 10': 0.36; '(from': 0.37; 'element': 0.37; 'but': 0.37; 'two': 0.37; 'received:org': 0.38; 'subject:: ': 0.39; 'getting': 0.39; 'header :Mime-Version:1': 0.39; 'to:addr:python.org': 0.39; 'ever': 0.65; 'dennis': 0.77
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject Re: How to isolate a constant?
Date Sat, 22 Oct 2011 17:55:26 -0700
Organization > Bestiaria Support Staff <
References <f5539538-d079-4be1-b2c0-d98d3fc6a33f@h39g2000prh.googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host user-11fb5l5.dsl.mindspring.com
X-Newsreader Forte Agent 3.3/32.846
X-No-Archive YES
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
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.2139.1319331340.27778.python-list@python.org> (permalink)
Lines 53
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1319331340 news.xs4all.nl 6900 [2001:888:2000:d::a6]:33017
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:14859

Show key headers only | View raw


On Sat, 22 Oct 2011 17:26:22 -0700 (PDT), Gnarlodious
<gnarlodious@gmail.com> declaimed the following in
gmane.comp.python.general:

> 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:

	You never copied it... You just created a temporary local reference
to the same object

	_someList = [0, 1]

creates a mutable list object containing two elements; it then binds the
/name/ "_someList" to that object.

	someList = self._someList

finds the OBJECT to which "self._someList" is bound, and binds a second
name "someList" to the same object.

	someList += "X"

retrieves the OBJECT to which "someList" is bound, mutates it by
appending an "X".

	Nowhere do you ever create a NEW list object. Try

	someList = self._someList[:]

which finds the object to which "self._someList" is bound, extracts a
sublist [:] (from first element to last element) -- this is a NEW list
-- and binds "someList" to that new object.

> Can someone explain this behavior? And how to prevent a classwide
> constant from ever getting changed?

	"Ever"? Can't really be done with Python -- if you know it exists,
you can get to it and rebind it, or mutate it if it is a mutable object.
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

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


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