Simple question - stock market simulation

Hendrik van Rooyen mail at microcorp.co.za
Tue Feb 10 02:48:53 EST 2009


"cptn.spoon" <cpt..n.... at gmail.com> wrote:
On Feb 9, 6:48 pm, "Hendrik van Rooyen" <m... at microcorp.co.za> wrote:

>> No.
>> At this level, just use a list of instances of your Stock class.
>>
>> - Hendrik
>
>How do I get a list of instances of a particular class? Is there a way
>to do this dynamically?

Yes there is. First make an empty list:

stock_market = []

Now when you create the instance:

stuff = GetStuffFromTheUserIfNecessary()
stock_market.append(Stock(stuff))

This leaves the new instance in the last position in the list.

When you have done that as many times as you wanted, 
to create the stocks in the market, you can do:

for stock in stock_market:
    DoThingsWithThisOneStock(stock)

>
>Also, what would be the way of dynamically creating an instance of a
>class based on user input (ie a user wants to create a new instance of
>the Stock class via shell input)?

This means your program must be looking for that input.
Either look at thread and threading, or write a simple
loop that gets input, appends a stock, gets more input
if required, does something with all the stocks, and
either loops or terminates. Or some variation of that
like first building the market and then manipulating it.

>
>I'm not sure if I have the wrong mindset here.
>

I honestly cannot tell if your mindset is right or
wrong - I suppose it depends on your definitions
of the two terms.
:-)
- Hendrik





More information about the Python-list mailing list