Simple question - stock market simulation

Terry Reedy tjreedy at udel.edu
Mon Feb 9 01:43:28 EST 2009


cptn.spoon wrote:
> On Feb 9, 3:58 pm, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
>> "cptn.spoon" <cptn.sp... at gmail.com> writes:
>>> I'm not asking for tips on the program itself, I just can't figure out
>>> how to get the data structures in place. Would I use Classes or would
>>> I use dictionaries? Am I completely off the mark with this?
>> Typically you would use a class for the multi-fielded data structure;
>> a dictionary is intended more for lookup tables where you don't know
>> ahead of time what the keys will be.  Also, you'd typically use a list
>> [1,2,3] rather than a tuple (1,2,3) to hold the price list.
> 
> Thanks Paul! I thought this might be the case. So how would I get the
> StockMarket class instance to contain many Stock class instances and
> then be able to iterate through them? I'm guessing the basic structure
> would be as follows...but I think I'm wrong:
> 
> class StockMarket:
>   pass
> 

I would start with using a list for StockMarket.

> class Stock:
>   def __init__(self, stockname, stockpricelist[], stockrisk,
> stockqty):
>     self.stockname = stockname
>     self.stockpricelist[] = stockpricelist[]
>     self.stockrisk = stockrisk
>     self.stockqty = stockqty

I would remove the redundant 'stock' from all the parameter and 
attribute names.  (And call 'qty' 'quantity'.)

You will iterating like so:

for stock in StockMarket:
   print(stock.name, stock.quantity,.... or whatever, and the iteration 
variable makes clear that a stock is a Stock.

tjr




More information about the Python-list mailing list