dictionary containing instances of classes behaving oddly

dictionary containing instances of classes behaving oddly

Post by Ben » Sat, 30 Dec 2006 02:16:18


Hello...

I have a dictionary, where each value is a seperate instance of the
same class:

self.mop_list[record_number]=record(self.mops[:])

In my case I know that record_number takes the values 0,3,and 7 thanks
to a loop, giving me three instances of the record class instantiaterd
with some data I've pased in.

The record object contains a list, and each element of that list should
also contain an instance of a class should I append one to it:


self.mop_list[record_number].my_list.append(my_class(0,0,0,self.mop_list[record_no].mops,0))

So within each record class I have access to any number of my_class
instances, depending on how many times I have appended:

self.mop_list[record_number].my_list[index]


This seems to work without any errors. But bizzarely I find that
whatever my record number, the instance of "my_class" is appended to
every list. So in this case

self.mop_list[0].my_list.append(my_class(Some data for the
constructor))

I would expect to append an instance of my_class to
self.mop_list[0].my_list

But annoyingly

self.mop_list[0].my_list
self.mop_list[3].my_list
self.mop_list[7].my_list

all have an instance of my_class created and appended to them. This is
really confusing and quite annoying - I don't know whether anyone out
there can make head or tail of what I'm doing wrong?

Cheers,

Ben
 
 
 

dictionary containing instances of classes behaving oddly

Post by Erik Johns » Sat, 30 Dec 2006 03:42:15


<snip>


Well, it's a little bit difficult, but I think I actually know what's going
on. You probably need some code that looks something like this, to ensure
each object has it's own, independent list:

class record:
def __init__(self, init_list=None):
self.my_list = []
if init_list is not None:
self.my_list.extend(init_list)


Here's what I think you are doing, and below should make it clear why that
doesn't work:

class record:
def __init__(self, init_list=[]):

That list above, the default initializer is constructed just once (when the
def statement executes)!

... def __init__(self, init_list=[]):
... self.my_list = init_list
...
[]
[]
['boo!']
[1, 2, 3]
[1, 2, 3]
[1, 2, 3]
[1, 2, 3, 42]
[1, 2, 3, 42]
[1, 2, 3, 42]

 
 
 

dictionary containing instances of classes behaving oddly

Post by Ben » Sat, 30 Dec 2006 04:13:38

Ah - ok. In fact I simply had:

class record:
my_list =[]
mops=[]

def __init__(self,mops):
self.mops=mops

Where mops is something I pass in when i create the instance. I had
thought that then each time I created an instance of the record class
as an element of my dictionary:

self.mop_list[record_number]=record(self.mops[:])

I would create a brand new instance of the record class.It would have a
mops list initialized with mops (because the def__init__ constructor is
called when the class is instantiated), and an empty, individual list
my_list.

I could then access each individual list by doing:

self.mop_list[x].my_list[y]=something

But in fact the same my_list is being accessed for all values of x, so
a change to one list is in fact a change to them all. I think you might
be right, but can't quite work it out myself! I'll keep trying :-)

Thanks for your help,

Ben
 
 
 

dictionary containing instances of classes behaving oddly

Post by Ben » Sat, 30 Dec 2006 04:18:52

h - I have been very silly indeed!

I had not realised that by creating my lists outside of the
def__init___() constructor they would always be class rather than
instance variables, and so there was only one of them when I referred
to it. Thanks to Erik and Chris for your help!

Ben


Ben wrote:


 
 
 

dictionary containing instances of classes behaving oddly

Post by Erik Johns » Sat, 30 Dec 2006 04:25:57


Similar to the example I gave, the lists my_list and mops shown above are
executed just once: when your class definition is first parsed.
The statement:

def __init__(self,mops):

is also executed just once, and the value for mops at that time is the value
assigned to object attributes during object construction - a reference to
record.mops, in your case. So, there are really only two lists here, class
attributes record.my_list and record.mops. Each of your constructed objects
is assigned a reference to record.mops. They all share that list. If you
want a record object to have it's own list, give it a new, empty one and
then populate it appropriately.
 
 
 

dictionary containing instances of classes behaving oddly

Post by Ben » Sat, 30 Dec 2006 04:37:39

Yes- I can see that my_list and mops, being outside def __init__()
only get created once, hence the confusion.

Surely def __init__() gets called each time an instance is created
however? But the snag is that if they have default values set these are
shared between all instances, even if they are, for instance, lists...?

Cheers,

Ben


Thanks.
 
 
 

dictionary containing instances of classes behaving oddly

Post by Erik Johns » Sat, 30 Dec 2006 06:13:06


I don't think I quite understand the confusion, and I think I might have
said something misleading. The def statement itself is only executed once,
as are any object initializers contained within it. Your record.__init__()
is not using a default initializer, and here:

class record:
my_list =[]
mops=[]

def __init__(self,mops):
self.mops=mops


mops as the second argument to __init__ is a formal vairable, not a
reference to record.mops. If you pass the same list reference to each
constructor, then each record object shares the same list.

You previously said:

I had thought that then each time I created an instance of the record class
as an element of my dictionary:

self.mop_list[record_number]=record(self.mops[:])

I would create a brand new instance of the record class.It would have a
mops list initialized with mops (because the def__init__ constructor is
called when the class is instantiated), and an empty, individual list
my_list.

I beleive this is basically correct - by using the slice notation in

self.mops[:]

you are passing a copy of that list as the value. The difference is
demonstrated here:

... X = [99]
... def __init__(self, X): # X is formal variable here, not Foo.X!
... self.X = X
...
[1, 2, 3]
[99]
[1, 2, 3, 42]
[1, 2, 3, 42]
[99]
[1, 2, 3, 42]
[1, 2, 3, 42]
[99]
[1, 2, 3, 42, 'something else']
[1, 2, 3, 42]
[1, 2, 3, 42]
[99]


So, I guess I don't see exactly where the problem is, and without all
your source, I am kinda poking in the dark. But from the behaviour observed
in your record objects, you are obviously ending up with shared list
references where you intend to get per-object individual lists. Generally
the way to do that is to assign a new, empty list in the body of the object
initializer and then populate that list as needed.

Good luck.
-ej
 
 
 

dictionary containing instances of classes behaving oddly

Post by Steven D'A » Sat, 30 Dec 2006 07:16:09


Others have already solved the immediate problem, but I'd just like to
make a brief comment about self-documenting code, in particular misleading
self-documenting code.

The O.P. says he has a DICTIONARY called mop_LIST.

[raises eyebrow]



--
Steven.