Re: dictionary containing instances of classes behaving oddly

2006-12-28 Thread Steven D'Aprano
On Thu, 28 Dec 2006 09:16:18 -0800, Ben wrote: > Hello... > > I have a dictionary, where each value is a seperate instance of the > same class: > > self.mop_list[record_number]=record(self.mops[:]) Others have already solved the immediate problem, but I'd just like to make a brief comment abou

Re: dictionary containing instances of classes behaving oddly

2006-12-28 Thread Erik Johnson
"Ben" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > 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

Re: dictionary containing instances of classes behaving oddly

2006-12-28 Thread Ben
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,

Re: dictionary containing instances of classes behaving oddly

2006-12-28 Thread Erik Johnson
"Ben" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > class record: > my_list =[] > mops=[] > > def __init__(self,mops): > self.mops=mops Similar to the example I gave, the lists my_list and mops shown above are executed just once: when your

Re: dictionary containing instances of classes behaving oddly

2006-12-28 Thread Ben
Ah - 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:

Re: dictionary containing instances of classes behaving oddly

2006-12-28 Thread Ben
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

Re: dictionary containing instances of classes behaving oddly

2006-12-28 Thread Erik Johnson
"Ben" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > 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 th

dictionary containing instances of classes behaving oddly

2006-12-28 Thread Ben
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