[Tutor] List of Classes with a dictionary within the Class.

2011-09-21 Thread Mukund Chavan

Hi,

I was trying to get a list of Class Objects. 
The Class itself has string fields and a dictionary that is initialized as a 
part of the "__init__"

However the fields of the dictionary for each object in the list of class 
objects seems to be the same.
I took an example from the following site:
"http://www.daniweb.com/software-development/python/code/216631"; and modified 
it. The code is cut/pasted below
with the result of the run immediately following. I expected the 4th and 5th 
column in the output to be unique, but
they seem to get the value of the last item in the iter range. 

I am running "Python 2.6.5" on Ubuntu 10.04 TLS-64bit.

Thanks in advance.

- Mukund


Code:

#!/usr/bin/python

#a list of class objects to mimic a C type array of structures
# tested with Python24 vegaseat 30sep2005

class Person(object):
   """__init__() functions as the class constructor"""
   personAttrs={"'num1":"","num1":""}
   def __init__(self, name=None, job=None, quote=None, num1=None, num2=None):
  self.name = name
  self.job = job
  self.quote = quote
  self.personAttrs["num1"]=num1
  self.personAttrs["num2"]=num2

print

# make a list of class Person(s)
personList = []
for i in range(4):
personList.append(Person("M",i,"XYZ",-i,i))

print "Show one particular item:"
print personList[0].name

print


print "... then show all quotes and who said so:"
for person in personList:
   print "\"%s\" %s (%s) %s %s" % (person.quote, person.name, 
person.job,str(person.personAttrs['num1']),str(person.personAttrs['num2']))

print


print "What the heck did the person named Sanosi say?"
look = "Sanosi"
for person in personList:
   if look in person.name:
  print "%s: \"%s\"" % (person.name, person.quote)



Output:

Show one particular item:
M

... then show all quotes and who said so:
"XYZ" M (0) -3 3
"XYZ" M (1) -3 3
"XYZ" M (2) -3 3
"XYZ" M (3) -3 3

What the heck did the person named Sanosi say?


  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List of Classes with a dictionary within the Class.

2011-09-21 Thread Mukund Chavan

That worked. Thank you for the explanation. 

I was trying to take a list of these objects and insert it as a part of a mongo 
document using the pymongo package. 

But based on what your input and little more reading up, I can use the 
"__dict__" method to accomplish the same.

Thanks again.

- Mukund


> Date: Thu, 22 Sep 2011 10:58:09 +1000
> From: st...@pearwood.info
> To: tutor@python.org
> Subject: Re: [Tutor] List of Classes with a dictionary within the Class.
> 
> Mukund Chavan wrote:
> > Hi,
> > 
> > I was trying to get a list of Class Objects. 
> > The Class itself has string fields and a dictionary that is initialized as 
> > a part of the "__init__"
> 
> No it doesn't. It has a dictionary that is initialised *once*, when the 
> class is defined. From that point on, every instance just modifies the 
> same shared dictionary:
> 
> > class Person(object):
> >"""__init__() functions as the class constructor"""
> >personAttrs={"'num1":"","num1":""}
> 
> This is a "class attribute", stored in the class itself, and shared 
> between all instances.
> 
> 
> >def __init__(self, name=None, job=None, quote=None, num1=None, 
> > num2=None):
> >   self.name = name
> >   self.job = job
> >   self.quote = quote
> >   self.personAttrs["num1"]=num1
> >   self.personAttrs["num2"]=num2
> 
> This merely modifies the existing class attribute. You want something 
> like this instead:
> 
> class Person(object):
>  def __init__(self, name=None, job=None, quote=None,
>   num1=None, num2=None
>  ):
>  self.name = name
>  self.job = job
>  self.quote = quote
>  self.personAttrs = {'num1': num1, 'num2': num2}
> 
> This creates a new dictionary for each Person instance.
> 
> But why are you doing it that way? Each Person instance *already* has 
> its own instance dictionary for storing attributes. You don't need to 
> manage it yourself -- just use ordinary attributes, exactly as you do 
> for name, job, and quote.
> 
> class Person(object):
>  def __init__(self, name=None, job=None, quote=None,
>   num1=None, num2=None
>  ):
>  self.name = name
>  self.job = job
>  self.quote = quote
>  self.num1 = num1
>  self.num2 = num2
> 
> 
> 
> -- 
> Steven
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor