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


Groups > comp.lang.python > #33179 > unrolled thread

Strange object identity problem

Started by"F.R." <anthra.norell@bluewin.ch>
First post2012-11-12 14:12 +0100
Last post2012-11-12 16:19 -0500
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  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

#33179 — Strange object identity problem

From"F.R." <anthra.norell@bluewin.ch>
Date2012-11-12 14:12 +0100
SubjectStrange object identity problem
Message-ID<mailman.3582.1352726004.27098.python-list@python.org>
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)

[toc] | [next] | [standalone]


#33183

FromUlrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Date2012-11-12 14:26 +0100
Message-ID<r0a7n9-mee.ln1@satorlaser.homedns.org>
In reply to#33179
Am 12.11.2012 14:12, schrieb F.R.:
> 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:
[...snip incomplete code...]
> Trying something similar with a simpler class works as expected:
[...snip example code...]

Okay, that's almost a classic. You ask about code that fails, while 
providing code that works as example. Crystal balls are rare nowadays, 
so this is really hard to answer!

In any case, here's what you could do:
1. use a debugger (import pdb...)
2. some more info could be retrieved by outputting the actual type along 
with the ID of the objects in question (see type() function)
3. reduce the non-working code until you have a minimal example that you 
can post here

I'd bet that at latest while trying approach 3 above, you will find the 
error yourself.

Good luck!

Uli

[toc] | [prev] | [next] | [standalone]


#33199 — Non Sequitur -- Re: Strange object identity problem

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2012-11-12 16:19 -0500
SubjectNon Sequitur -- Re: Strange object identity problem
Message-ID<mailman.3593.1352755136.27098.python-list@python.org>
In reply to#33183
On Mon, 12 Nov 2012 14:26:19 +0100, Ulrich Eckhardt
<ulrich.eckhardt@dominolaser.com> declaimed the following in
gmane.comp.python.general:


> 
> Okay, that's almost a classic. You ask about code that fails, while 
> providing code that works as example. Crystal balls are rare nowadays, 

	Not that rare -- I have six*... Spending time to condition and
attune the ball, OTOH, I've not done...

> so this is really hard to answer!
> 

	Not hard to answer -- just gazing at my LCD display reveals the OP
has

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

in which they are looping/printing on "y", but are extracting the SAME
element using "year"

	The middle statement should be

	b = bas[y]


-=-=-=-=-=-
*	Well, okay -- one "crystal ball" is acrylic, and three others are
reconstituted [ie; melted junk quartz recast in larger size, but not
really grown crystals] quartz crystal (
http://www.gaelsong.com/product/7173/decor-other ); the two natural
stones are only about an inch in diameter and the most expensive.
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web