[Tutor] OOPs?
Bryce Embry
bryce@bembry.org
Fri, 29 Mar 2002 08:27:41 -0600
Howdy,
I'm trying to get the hang of classes and would like to tap into the wealth
of this tutorial for some guidance. I've looked through a number of books
and tutorials, so I understand how to construct a class, but I'm not sure
how and why to use one. So, I am creating a small "stock tracker" program
specifically to learn how to use classes. I'm not going to use it for
anything (I don't have the money to own stock), this is really just a
pedantic example for learning. I've started building it, but run into a
problem. Can anyone give me an idea of where my understanding has gone off
track, or how to make this concept work?
I have made a class that looks like the following:
class Stock:
stocklist = []
def __init__(self, name, price, shares):
# get name of stock, initial price, initial number of shares
# stick the new stock name in Stock.stocklist
def update (self, newprice, newshares):
# change the shares in a stock
def otherstuff(self):
# do other stuff, like print current value, etc.
My thought was to design the program so that each time the user added a
stock to the portfolio, the program would create a new instance of the
class. I don't know how to do that, though. I want what in PHP is a
"variable variable", where I can do something like this:
newstock = raw_input("Enter name of new stock: ")
newstock = Stock()
So, if the person chose IBM, I would have an instance called
IBM.Stock(). If he typed in RHAT, I'd have another instance called
RHAT.Stock(). Then, I could update, track, and do other stuff by calling
the instance of the class. And, if I want a list of all my stocks, I'd
just call Stock.stocklist .
Is this even the right way to use a class? I know I could get the job done
with functions, dictionaries and lists without using a class, but I'm
trying to understand classes. Any suggestions or insights?
Thanks,
Bryce Embry
Geek-Of-All-Trades / Master-Of-None
--------------------------------------------------
"Lord, you have established peace for us.
All that we have accomplished you have done for us" -- Isaiah 26:12