dictionary containing instances of classes behaving oddly

Ben Benjamin.Barker at gmail.com
Thu Dec 28 12:16:18 EST 2006


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




More information about the Python-list mailing list