Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > del class with recursive list

Reply
Thread Tools

del class with recursive list

 
 
duccio
Guest
Posts: n/a
 
      03-08-2008
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=[]
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())

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

 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      03-08-2008
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
 
Reply With Quote
 
 
 
 
duvo@tiscali.it
Guest
Posts: n/a
 
      03-09-2008

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

 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      03-10-2008
On 9 mar, 17:20, (E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
del an imported Class at EOF... why? Ryan Python 12 10-08-2009 08:20 PM
Recursive functions Vs Non-recursive functions - performance aspect vamsi C Programming 21 03-09-2009 10:53 PM
Two recursive calls inside of a recursive function n00m C++ 12 03-13-2008 03:18 PM
after del list , when I use it again, prompt 'not defined'.how could i delete its element,but not itself? python Python 7 06-03-2006 05:08 PM
How to del item of a list in loop? Reinhold Birkenfeld Python 25 01-17-2005 02:07 AM



Advertisments