Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. import gc
  2.  
  3. # create a simple object that links to itself
  4. class Node:
  5.  
  6. def __init__(self, name):
  7. self.name = name
  8. self.parent = None
  9. self.children = []
  10.  
  11. def addchild(self, node):
  12. node.parent = self
  13. self.children.append(node)
  14.  
  15. def __repr__(self):
  16. return "<Node %s at %x>" % (repr(self.name), id(self))
  17.  
  18. # set up a self-referencing structure
  19. root = Node("monty")
  20.  
  21. root.addchild(Node("eric"))
  22. root.addchild(Node("john"))
  23. root.addchild(Node("michael"))
  24.  
  25. # remove our only reference
  26. del root
  27.  
  28. print gc.collect(), "unreachable objects"
  29. print gc.collect(), "unreachable objects"
  30.  
  31. ## 12 unreachable objects
  32. ## 0 unreachable objects
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement