F
F.R.
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:
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
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:
def __init__ (self, i):
self.i = i
def run (self):
self.ii = self.i * self.i
c = C (year)
c.run ()
print year, id (c)
cees [year] = c
2010 150837804
2011 148275756
2012 146131212
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)
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:
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
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:
def __init__ (self, i):
self.i = i
def run (self):
self.ii = self.i * self.i
c = C (year)
c.run ()
print year, id (c)
cees [year] = c
2010 150837804
2011 148275756
2012 146131212
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)