python class instantiation
pandyacus.xspam at xspam.sbcglobal.net
Tue Oct 24 00:35:12 CEST 2006
Éric Daigneault wrote:
>Got a question for you all...
>I noticed a behaviour in python class creation that is strange, to say the
>When creating a class with data members but no __init__ method. Python deals
>differently with data members that are muatable and immutables.
> stringData = "Whatever"
> listData = 
>instance = A()
>Will have data members instantiated as expected (instance.stringData ==
>"Whatever" and instance.listData == )
>instance.stringData = "Changed"
>Now if I do
>secondInstance = A()
>it will come with the listData containing the SomeString appended to the
>this is clearly not normal
>Especially that the stringData of Second instance contains the "Whatever" text.
>If the behaviour was at least consistant... but it is not...
>Am I coing nuts or is this by desing, if so it is very misleading... The two
>instances are sharing the same list, but not the same string ... I did not
>declare the list to be static in any way.... Why does it behave like this ?
This is not what I get :
Here is the code and the output for 2.5
stringData = "Whatever"
listData = 
inst = A()
inst.stringData = "Changed"
inst2 = A()
inst.stringData = "NewChanged"
Isn't this what you got?
More information about the Python-list