[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

del class with recursive list

duccio

3/8/2008 6:35:00 PM

Hello!
Will someone have time to tell me why this code don't work as I expect?
And what should I do to make the "del n" delete all the lower nodes? =

Thanks!

class Node:
def __init__(self):
self.childs=3D[]
def appendNode(self, n):
self.childs.append(n)
def __del__(self):
print 'del', id(self)

n =3D Node()
for i in range(5):
n.appendNode(Node())
for nodes in n.childs:
nodes.appendNode(Node())

del n

print '--------end--------'


gives this:


del 10965280
del 10965440
del 10965640
del 10965400
del 10965600
del 10965360
del 10965560
del 10965320
del 10965520
--------end--------
del 10965480
del 10965680

3 Answers

Peter Otten

3/8/2008 6:37:00 PM

0

duccio wrote:

> Will someone have time to tell me why this code don't work as I expect?
> And what should I do to make the "del n" delete all the lower nodes?
> Thanks!
>
> class Node:
> def __init__(self):
> self.childs=[]
> def appendNode(self, n):
> self.childs.append(n)
> def __del__(self):
> print 'del', id(self)
>
> n = Node()
> for i in range(5):
> n.appendNode(Node())
> for nodes in n.childs:
> nodes.appendNode(Node())

# you forgot a reference to a child node and its child:
del nodes

> del n
>
> print '--------end--------'
>
>
> gives this:
>
>
> del 10965280
> del 10965440
> del 10965640
> del 10965400
> del 10965600
> del 10965360
> del 10965560
> del 10965320
> del 10965520
> --------end--------
> del 10965480
> del 10965680

Peter

duccio

3/9/2008 8:20:00 PM

0


Thanks! I just need to remember to del the variables after "for in".

Gabriel Genellina

3/10/2008 5:10:00 AM

0

On 9 mar, 17:20, d...@tiscali.it wrote:

> Thanks! I just need to remember to del the variables after "for in".

And when working on the interactive interpreter, it's easy to forget
the _ variable too (that holds the last printed expression)

--
Gabriel Genellina