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


Groups > comp.lang.python > #33179

Strange object identity problem

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.mixmin.net!feed.xsnews.nl!border-2.ams.xsnews.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <anthra.norell@bluewin.ch>
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.05; 'none,': 0.05; '(self,': 0.07; '(self):': 0.09; '__init__': 0.09; 'behave': 0.09; 'loop.': 0.09; 'suggestions.': 0.09; 'def': 0.10; '2.7': 0.13; '"st"': 0.16; 'attributes,': 0.16; 'clashes': 0.16; 'identities.': 0.16; 'received:195.186': 0.16; 'received:bluewin.ch': 0.16; 'subject:object': 0.16; '>>>': 0.18; 'module': 0.19; 'all,': 0.21; 'trying': 0.21; '(b)': 0.22; 'keys': 0.22; 'simpler': 0.22; 'subject:problem': 0.22; 'runs': 0.22; 'example': 0.23; 'header:User-Agent:1': 0.26; 'wondering': 0.26; 'object,': 0.27; "doesn't": 0.28; 'run': 0.28; 'arguments.': 0.29; 'dictionary': 0.29; 'routine': 0.29; 'up:': 0.29; 'objects': 0.29; 'class': 0.29; 'checked': 0.30; 'stuff': 0.30; 'year,': 0.30; 'print': 0.32; 'turns': 0.33; 'ubuntu': 0.33; 'to:addr:python- list': 0.33; '(c)': 0.33; 'skip:- 20': 0.34; 'done': 0.34; 'list': 0.35; 'identity': 0.35; 'similar': 0.35; 'something': 0.35; 'next': 0.35; 'created': 0.36; 'method': 0.36; 'being': 0.37; 'why': 0.37; 'data': 0.37; 'store': 0.38; 'object': 0.38; 'shows': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'skip:" 10': 0.40; 'received:192.168': 0.40; 'range': 0.60; 'containing': 0.61; 'different': 0.63; 'here': 0.65; 'records.': 0.65; 'hours': 0.66; 'received:ch': 0.69; 'verification': 0.72; 'positions:': 0.84; 'verifying': 0.84; 'desperate': 0.91
Date Mon, 12 Nov 2012 14:12:08 +0100
From "F.R." <anthra.norell@bluewin.ch>
User-Agent Mozilla/5.0 (X11; Linux i686; rv:16.0) Gecko/20121028 Thunderbird/16.0.2
MIME-Version 1.0
To python-list@python.org
Subject Strange object identity problem
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.3582.1352726004.27098.python-list@python.org> (permalink)
Lines 79
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1352726004 news.xs4all.nl 6963 [2001:888:2000:d::a6]:56867
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:33179

Show key headers only | View raw


Hi all,

Once in a while I write simple routine stuff and spend the next few hours
trying to understand why it doesn't behave as I expect. Here is an example
holding me up: I have a module "st" with a class "runs". In a loop I 
repeatedly
create an object "ba" and call the method "ba.run ()" which processes the
constructor's arguments. Next I store the object in a dictionary "bas". It
then turns out that all keys hold the same object, namely the one created
last in the loop.
      Verifying the identity of each object when it is being assigned to
the dictionary reveals different identities. Repeating the verification
after the loop is done shows the same object in all key positions:

 >>> bas = {}
 >>> for year in range (2010, 2013):
     ba = st.runs ('BA', '%d-01-01' % year, '%d-12-31' % year)
     ba.run ()
print year, id (ba)
     bas [year] = ba

2010 150289932
2011 150835852
2012 149727788

 >>> for y in sorted (bas.keys ()):
     b = bas [year]
     print y, id (b)

2010 149727788
2011 149727788
2012 149727788

--------------------

The class "runs" has a bunch of attributes, among which an object 
"parameters"
for tweaking processing runs and a object "quotes" containing a list of 
data
base records. Both objects are created by "runs.__init__ (...)".

Trying something similar with a simpler class works as expected:

 >>> class C:
     def __init__ (self, i):
         self.i = i
     def run (self):
         self.ii = self.i * self.i

 >>> cees = {}
 >>> for year in range (2010, 2013):
     c = C (year)
     c.run ()
     print year, id (c)
     cees [year] = c

2010 150837804
2011 148275756
2012 146131212

 >>> for year in sorted (cees.keys ()):
     print year, id (cees [year]), cees [year].ii

2010 150837804 4040100
2011 148275756 4044121
2012 146131212 4048144

--------------------


I have checked for name clashes and found none, wondering what to check
next for. Desperate for suggestions.


Frederic


(Python 2.7 on Ubuntu 12.04)

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


Thread

Strange object identity problem "F.R." <anthra.norell@bluewin.ch> - 2012-11-12 14:12 +0100
  Re: Strange object identity problem Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-11-12 14:26 +0100
    Non Sequitur -- Re: Strange object identity problem Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-11-12 16:19 -0500

csiph-web