Why the result

HYRY zhangry at feng.co.jp
Fri Oct 20 11:06:47 CEST 2006


Why the third print stement output "'comments': [(1, 2, 3)]", I think
it should be [].
I am using
Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)]
on win32.

# program
class PicInfo:
    def __init__(self, intro="", tags="", comments=[]):
        self.picintro = intro
        self.pictags = tags
        self.comments = comments

    def addcomment(self, comment):
        self.comments.insert(0, comment)

dbdata = PicInfo()
print dbdata.__dict__
dbdata.addcomment((1,2,3))
print dbdata.__dict__
dbdata = PicInfo()
print dbdata.__dict__

# output
{'pictags': '', 'comments': [], 'picintro': ''}
{'pictags': '', 'comments': [(1, 2, 3)], 'picintro': ''}
{'pictags': '', 'comments': [(1, 2, 3)], 'picintro': ''} <- why
comments is not []?




More information about the Python-list mailing list