[EMAIL PROTECTED] schrieb: > I have written a program that runs portfolio simulations with > different parameters and prints the output, but am mystified by the > behavior of a mutable class variable. A simplified version of the > program follows - would you kindly help me understand why it behaves > the way it does. > > The function main() reads some data and then repeatedly calls > simulation() with different parameter values. Each time the simulation > runs, it creates a collection of stocks, simulates their behavior and > prints the results. > > Here's what I expect to happen each time simulation( ) is called: the > class variable NStocks for the class Stock is initialized to an empty > list, and is then built up by __init__ as stocks are added to the > portfolio. Unfortunately, ths is not what actuallly happens .NStocks > is initialized to an empty list and then built up as I expect on the > first call to simulation( ), but appears to persists between calls to > simulation( ). > > Question: Why? Do I not create an entirely new list of stock objects > each time I enter simulation()? I am aware that mutable items can > behave in tricky ways, but am thoroughly mystified by the persistence > of NStocks between calls to simulation()
http://www.python.org/doc/faq/general.html#why-are-default-values-shared-between-objects Diez -- http://mail.python.org/mailman/listinfo/python-list