python class instantiation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Éric Daigneault lists

    #1

    python class instantiation

    Got a question for you all...

    I noticed a behaviour in python class creation that is strange, to say
    the least.

    When creating a class with data members but no __init__ method. Python
    deals differently with data members that are muatable and immutables.

    Ex:
    class A(object):
    stringData = "Whatever"
    listData = []

    instance = A()

    Will have data members instantiated as expected (instance.strin gData ==
    "Whatever" and instance.listDa ta == [])

    instance.listDa ta.append("Some String")
    instance.string Data = "Changed"

    Now if I do

    secondInstance = A()

    it will come with the listData containing the SomeString appended to the
    instance...

    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 ?

    Éric
  • Chetan

    #2
    Re: python class instantiation

    Éric Daigneault wrote:
    >Got a question for you all...
    >
    >I noticed a behaviour in python class creation that is strange, to say the
    >least.
    >
    >When creating a class with data members but no __init__ method. Python deals
    >differently with data members that are muatable and immutables.
    >
    >Ex:
    >class A(object):
    stringData = "Whatever"
    listData = []
    >
    >instance = A()
    >
    >Will have data members instantiated as expected (instance.strin gData ==
    >"Whatever" and instance.listDa ta == [])
    >
    >instance.listD ata.append("Som eString")
    >instance.strin gData = "Changed"
    >
    >Now if I do
    >
    >secondInstan ce = A()
    >
    >it will come with the listData containing the SomeString appended to the
    >instance...
    >
    >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 ?
    >
    >Éric
    This is not what I get :
    Here is the code and the output for 2.5
    class A(object):
    stringData = "Whatever"
    listData = []

    inst = A()

    print inst.stringData
    print inst.listData
    print

    inst.listData.a ppend("SomeStri ng")
    inst.stringData = "Changed"

    inst2 = A()

    print inst2.stringDat a
    print inst2.listData
    print

    inst.listData.a ppend("NewThing ")
    inst.stringData = "NewChanged "

    print inst.stringData
    print inst.listData
    print inst.stringData
    print inst.listData
    print

    -----
    Whatever
    []

    Whatever
    ['SomeString']

    NewChanged
    ['SomeString', 'NewThing']
    NewChanged
    ['SomeString', 'NewThing']

    Isn't this what you got?

    -Chetan

    Comment

    Working...