Two instances share an attribute

Filip Gruszczyński gruszczy at gmail.com
Sat Nov 15 16:41:44 EST 2008


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

#!/usr/bin/python

class EnumeratedContent:

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

        def values(self):
                return self.values_

        def addValue(self, value):
                self.values_.append(value)

x = EnumeratedContent()
x.addValue('ff')
print x.values()
x = EnumeratedContent()
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


More information about the Python-list mailing list