Two instances share an attribute

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?UTF-8?Q?Filip_Gruszczy=C5=84ski?=

    Two instances share an attribute

    I really don't understand, what's happening with the following code.
    Am I doing something wrong?

    #!/usr/bin/python

    class EnumeratedConte nt:

    def __init__(self, values = []):
    self.values_ = values

    def values(self):
    return self.values_

    def addValue(self, value):
    self.values_.ap pend(value)

    x = EnumeratedConte nt()
    x.addValue('ff' )
    print x.values()
    x = EnumeratedConte nt()
    x.addValue('gg' )
    print x.values()

    This code prints:

    ['ff']
    ['ff', 'gg']

    Why the heck self.__values keeps old list? Can anyone explain it to me?

    I am using:

    Python 2.5.2 (r252:60911, Sep 14 2008, 23:49:00)
    [GCC 4.1.2 (Gentoo 4.1.2 p1.0.2)] on linux2


    --
    Filip Gruszczyński
Working...