copy.deepcopy(): is it bug or intended behavior?

Benjamin Han bhan at andrew.cmu.edu
Wed Jul 31 19:59:57 EDT 2002


This is an abstract of the actual code:

--- cut here ---
import copy

class Foo:
    def clone (self):
        return copy.deepcopy(self)

class Bar (list):
    pass

class Const:
    def __init__ (self, data):
        self.data=data
    def __repr__ (self):
        return str(self.data)

n1=Foo()
n1.t=[]
c=Bar()
c.append(Const('1'))
n1.t.append(c)

n2=Foo()
n2.t=[]
c=Bar()
c.append(Const('2'))
n2.t.append(c)

g=Foo()
g.hd=[]
g.hd.append(n1)
g.hd.append(n2)

print g.hd[0].t,id(g.hd[0].t)
print g.hd[1].t,id(g.hd[1].t)

print '----------------------------------------------------------------------'

h=g.clone()

print h.hd[0].t,id(h.hd[0].t)
print h.hd[1].t,id(h.hd[1].t)

--- cut here ---


Running on Python 2.2.1 gave the following output:

[[1]] 135743596
[[2]] 135759124
----------------------------------------------------------------------
[[1]] 135505252
[[1]] 135793916


But the last line should have "[[2]]...". Am I missing something here?

Thanks,

Ben




More information about the Python-list mailing list