Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 28th, 2006, 8:07 PM   #1
MR.T
Newbie
 
MR.T's Avatar
 
Join Date: Feb 2006
Location: USA,Michigan
Posts: 22
Rep Power: 0 MR.T is on a distinguished road
Classes question

I'm having a bit of trouble figuring out classes. Here's a rough example of what I'm trying to do:

class Item_Class:
def __int__(self):
self.Name
self.Description
self.Price

List[]
List.append(Item_Class)
List[0].Name = "Something"
List[0].Description = "Something"
List[0].Price = "Something"

That works fine. But if I append the class to the list a second time, and assign some values to it, the previous instance of the class is changed to those values also.
What am I doing wrong? Isn't the idea of classes to be able to make different instances of it?

thanks
__________________
"Gee, Brain, what do you want to do tonight?" "The same thing we do every night, Pinky: Try to take over the world!"
MR.T is offline   Reply With Quote
Old Oct 28th, 2006, 8:36 PM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,835
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
Old Oct 28th, 2006, 9:26 PM   #3
MR.T
Newbie
 
MR.T's Avatar
 
Join Date: Feb 2006
Location: USA,Michigan
Posts: 22
Rep Power: 0 MR.T is on a distinguished road
Ahh, that makes sense. Thank you very much for simple and clear example.
On a related note were can I find out more about the different ways classes are used in programming?

BTW sorry about not puting the code in tags, I was going to but forgot because I was doing a couple things at the same time(maybe I should cut down on the Dr.pepper).

thanks again
__________________
"Gee, Brain, what do you want to do tonight?" "The same thing we do every night, Pinky: Try to take over the world!"
MR.T is offline   Reply With Quote
Old Oct 28th, 2006, 9:34 PM   #4
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,835
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
The way classes are used in programming are for you to discover and apply yourself as a programmer. The applications of Object-Oriented programming are very vast, and can be applied basically anywhere. But the more you use it as a programmer, the more you'll see where it should and should not be applied.

Otherwise, I hope wherever you're learning classes from explains the general applications of classes, and where and where not to use them.
Sane is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointers to std::strings Question (and classes in general) Soulstorm C++ 3 Aug 1st, 2006 5:15 AM
Attitudes Oddball Coder's Corner Lounge 29 Mar 18th, 2006 9:34 PM
Could some please explain classes to me... TCStyle C++ 10 Feb 20th, 2006 3:51 PM
How to post a question nnxion C++ 10 Jun 3rd, 2005 11:53 AM
Nested Classes Question Arla C# 1 Apr 7th, 2005 4:05 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:11 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC