![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2006
Location: USA,Michigan
Posts: 22
Rep Power: 0
![]() |
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!" |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,799
Rep Power: 5
![]() |
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].PriceHowever, 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].PriceFurthermore 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. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Feb 2006
Location: USA,Michigan
Posts: 22
Rep Power: 0
![]() |
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!" |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,799
Rep Power: 5
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |