Problem when subclass instance changes base class instance variable

Discussion in 'Python' started by Gerry Sutton, Apr 16, 2005.

  1. Gerry Sutton

    Gerry Sutton Guest

    Hi All!

    I have noticed a strange behavior when using a constant identifier to
    initialize an instance list variable in a base class and then trying to
    modifying the list in subclasses by using either the list.extend method or
    even by having the subclass create a whole new list in the variable.

    The following example illustrates the situation.



    Lbase = ['Initial Base Data']

    LSub1 = ['SubClass 1 data']

    LSub2 = ['SubClass 2 data']



    ##

    class BaseClass:



    def __init__(self):

    self.data = Lbase #<---- this fails??

    # self.data = ['Initial Base Data'] #<---- this works

    def printData(self):

    print self.data



    ##

    class SubClass1(BaseClass):



    def __init__(self):

    BaseClass.__init__(self)

    self.data.extend(LSub1)

    # self.data += LSub1



    ##

    class SubClass2(BaseClass):



    def __init__(self):

    BaseClass.__init__(self)

    self.data.extend(LSub2)

    # self.data += LSub2



    s1 = SubClass1()

    s1.printData()



    s2 = SubClass2()

    s2.printData()



    s11 = SubClass1()

    s11.printData()



    s21 = SubClass2()

    s21.printData()

    s1.printData()



    >>>


    ['Initial Base Data', 'SubClass 1 data']

    ['Initial Base Data', 'SubClass 1 data', 'SubClass 2 data']

    ['Initial Base Data', 'SubClass 1 data', 'SubClass 2 data', 'SubClass 1
    data']

    ['Initial Base Data', 'SubClass 1 data', 'SubClass 2 data', 'SubClass 1
    data', 'SubClass 2 data']

    ['Initial Base Data', 'SubClass 1 data', 'SubClass 2 data', 'SubClass 1
    data', 'SubClass 2 data']

    >>>


    It looks like the instance variable is acting like a class variable with all
    the subclass changes accumulating as more objects are created.

    Does anyone have an explanation for this behavior?

    Are other mutable sequence types affected the same way?

    To fix the problem you simply use a literal list to initialize the variable
    in the base class instead of an identifier.

    This problem does not exist if the data variable is a string, maybe because
    it is immutable?

    I am using Python Ver 2.3.

    Does this code behave the same way in 2.4,
     
    Gerry Sutton, Apr 16, 2005
    #1
    1. Advertising

  2. Gerry Sutton

    Peter Otten Guest

    Gerry Sutton wrote:

    > I have noticed a strange behavior when using a constant identifier to
    > initialize an instance list variable in a base class and then trying to
    > modifying the list in subclasses by using either the list.extend method or
    > even by having the subclass create a whole new list in the variable.
    >
    > The following example illustrates the situation.


    > Lbase = ['Initial Base Data']


    > class BaseClass:
    > def __init__(self):
    > self.data = Lbase                 #<----  this fails??


    self.data and Lbase are now bound to the same variable. Consequently changes
    to that variable through self.data affect Lbase and vice versa. This is
    standard Python behaviour. If you want to use Lbase as a kind of initial
    value, modify BaseClass to make a (shallow) copy:

    class BaseClass:
    def __init__(self):
    self.data = list(Lbase)

    Now every BaseClass instance gets its separate copy. If you have nested
    mutables, e. g. a list of lists, look into the copy module for deepcopy().

    Peter
     
    Peter Otten, Apr 16, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jstorta
    Replies:
    3
    Views:
    449
    jstorta
    Feb 20, 2006
  2. Sridhar R
    Replies:
    14
    Views:
    1,414
    =?iso-8859-1?Q?Fran=E7ois?= Pinard
    Feb 10, 2004
  3. Sandra-24
    Replies:
    18
    Views:
    1,879
    Steve Holden
    Apr 29, 2006
  4. Lou Pecora
    Replies:
    8
    Views:
    345
    Lou Pecora
    May 25, 2006
  5. Trans
    Replies:
    8
    Views:
    324
    Robert Klemme
    Oct 23, 2008
Loading...

Share This Page