Behavior of mutable class variables

castironpi at gmail.com castironpi at gmail.com
Wed May 9 19:23:39 EDT 2007


On May 9, 5:49 pm, tkp... at hotmail.com wrote:
> Thanks for the insights. I solved the problem as follows: I created a
> new class method called cleanUp, which resets NStocks to an empty list
> and N1 to 0. Works like a charm - it's the first time I've used a
> class method, and I immediately see its utility. Thanks again
>
> class Stock(object):
>     NStocks = []             #Class variables
>     N1         = 0
>
>     @classmethod
>     def cleanUp(cls):
>         Stocks.NStocks = []
>         Stocks.N1 = 0
>
> def simulation(N, par1, par2, idList, returnHistoryDir):
>
>     Stock.cleanUp()
>     results = ......
>     print results.

class A:
	b= 0

A.b
a= A()
a.b
a.b+= 1
a.b
A.b
A.b=20
a.b
A.b
a1= A()
a1.b
a.b
A.b
a1.b+=10
a1.b
a.b
A.b

It looks like an instance gets its own copy of A's dictionary upon
creation, and -can- no longer affect A's dictionary, though both can
be changed elsewhere.

Doesn't seem prudent to -obscure- a class by an instance, but if
that's not what goes on, then I'm missing something.




More information about the Python-list mailing list