del class with recursive list

2008-03-08 Thread duccio
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

-- 
http://mail.python.org/mailman/listinfo/python-list


__iter__ yield

2008-03-09 Thread duccio

Hello!
Someone knows if it's possible to make this __iter__ function with just  
one 'yield' intead of two?
Is there some simpler way to make this  __iter__ iter through all nodes?
Thanks!

class Node:
 def __init__(self, data=None):
 self.childs=[]
 self.data=data
 def appendNode(self, n):
 node=Node(n)
 self.childs.append(node)
 return node
 def __str__(self):
 return '<'+str(self.data)+'>'
 def __iter__(self):
 yield self #1
 for n in self.childs:
 for nn in n.__iter__():
 yield nn #2

n=Node()
n.appendNode(1).appendNode(2).appendNode(3).appendNode(4)
n.appendNode(11).appendNode(22).appendNode(33).appendNode(44)
for node in n:
 print node
-- 
http://mail.python.org/mailman/listinfo/python-list