<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Tahoma
}
--></style>
</head>
<body class='hmmessage'><div dir='ltr'>
That worked. Thank you for the explanation. <br><br>I was trying to take a list of these objects and insert it as a part of a mongo document using the pymongo package. <br><br>But based on what your input and little more reading up, I can use the "__dict__" method to accomplish the same.<br><br>Thanks again.<br><br>- Mukund<br><br><br><div>> Date: Thu, 22 Sep 2011 10:58:09 +1000<br>> From: steve@pearwood.info<br>> To: tutor@python.org<br>> Subject: Re: [Tutor] List of Classes with a dictionary within the Class.<br>> <br>> Mukund Chavan wrote:<br>> > Hi,<br>> > <br>> > I was trying to get a list of Class Objects. <br>> > The Class itself has string fields and a dictionary that is initialized as a part of the "__init__"<br>> <br>> No it doesn't. It has a dictionary that is initialised *once*, when the <br>> class is defined. From that point on, every instance just modifies the <br>> same shared dictionary:<br>> <br>> > class Person(object):<br>> > """__init__() functions as the class constructor"""<br>> > personAttrs={"'num1":"","num1":""}<br>> <br>> This is a "class attribute", stored in the class itself, and shared <br>> between all instances.<br>> <br>> <br>> > def __init__(self, name=None, job=None, quote=None, num1=None, num2=None):<br>> > self.name = name<br>> > self.job = job<br>> > self.quote = quote<br>> > self.personAttrs["num1"]=num1<br>> > self.personAttrs["num2"]=num2<br>> <br>> This merely modifies the existing class attribute. You want something <br>> like this instead:<br>> <br>> class Person(object):<br>> def __init__(self, name=None, job=None, quote=None,<br>> num1=None, num2=None<br>> ):<br>> self.name = name<br>> self.job = job<br>> self.quote = quote<br>> self.personAttrs = {'num1': num1, 'num2': num2}<br>> <br>> This creates a new dictionary for each Person instance.<br>> <br>> But why are you doing it that way? Each Person instance *already* has <br>> its own instance dictionary for storing attributes. You don't need to <br>> manage it yourself -- just use ordinary attributes, exactly as you do <br>> for name, job, and quote.<br>> <br>> class Person(object):<br>> def __init__(self, name=None, job=None, quote=None,<br>> num1=None, num2=None<br>> ):<br>> self.name = name<br>> self.job = job<br>> self.quote = quote<br>> self.num1 = num1<br>> self.num2 = num2<br>> <br>> <br>> <br>> -- <br>> Steven<br>> _______________________________________________<br>> Tutor maillist - Tutor@python.org<br>> To unsubscribe or change subscription options:<br>> http://mail.python.org/mailman/listinfo/tutor<br></div>                                            </div></body>
</html>