View Single Post
Old Oct 28th, 2006, 8:36 PM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,886
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
You need to change "List.append(Item_Class)" to "List.append(Item_Class())". The () initiates a new object for that class. You were appending to the list the direct reference to the class.

The following is a working example...

Note that the class's contents are completely blank. This is because you're declaring and initiating the variables externally. There is no need to put anything inside.

class Item_Class:
    pass

List = []

List.append(Item_Class())

List[0].Name = "IDLE"
List[0].Description = "Python Interpreter"
List[0].Price = 0.00


List.append(Item_Class())

List[1].Name = "My Help"
List[1].Description = "Programming Help From Sane"
List[1].Price = 0.00


print List[0].Name
print List[0].Description
print List[0].Price

print List[1].Name
print List[1].Description
print List[1].Price

However, if you want to write it in a way that provides a better understanding of how classes work, then consider the following...

class Item_Class:
    def __init__(self, Name, Description, Price):
        self.Name = Name
        self.Description = Description
        self.Price = Price

List = []

List.append(Item_Class("IDLE", "Python Interpreter", 0.00))
List.append(Item_Class("My Help", "Programming Help From Sane", 0.00))

print List[0].Name
print List[0].Description
print List[0].Price

print List[1].Name
print List[1].Description
print List[1].Price

Furthermore expanding on the program, we can get the same output like so...
class Item_Class:
    def __init__(self, Name, Description, Price):
        self.Name = Name
        self.Description = Description
        self.Price = Price

    def Output(self):
        print self.Name
        print self.Description
        print self.Price

List = []

List.append(Item_Class("IDLE", "Python Interpreter", 0.00))
List.append(Item_Class("My Help", "Programming Help From Sane", 0.00))

List[0].Output()
List[1].Output()

If anything I'm doing confuses you, don't hesitate to ask any questions. I also strongly suggest that you use the search to look through the Python forum for more examples using classes. I believe there's some excellent information provided by Arevos lying around.

I also urge you to use [code][/code] tags next time you post code.
Sane is offline   Reply With Quote