del class with recursive list

Discussion in 'Python' started by duccio, Mar 8, 2008.

  1. duccio

    duccio Guest

    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
    duccio, Mar 8, 2008
    #1
    1. Advertising

  2. duccio

    Peter Otten Guest

    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
    Peter Otten, Mar 8, 2008
    #2
    1. Advertising

  3. duccio

    Guest

    Thanks! I just need to remember to del the variables after "for in".
    , Mar 9, 2008
    #3
  4. On 9 mar, 17:20, 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
    Gabriel Genellina, Mar 10, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Reinhold Birkenfeld

    How to del item of a list in loop?

    Reinhold Birkenfeld, Jan 15, 2005, in forum: Python
    Replies:
    25
    Views:
    637
    skull
    Jan 17, 2005
  2. python
    Replies:
    7
    Views:
    304
    Bruno Desthuilliers
    Jun 3, 2006
  3. n00m
    Replies:
    12
    Views:
    1,113
  4. vamsi
    Replies:
    21
    Views:
    2,073
    Keith Thompson
    Mar 9, 2009
  5. Ryan
    Replies:
    12
    Views:
    483
    Terry Reedy
    Oct 8, 2009
Loading...

Share This Page