code_junkie

Python object deleting itself

Nov 12th, 2011
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. >>> class A():
  2. def kill(self):
  3. del self
  4.  
  5.  
  6. >>> a = A()
  7. >>> a.kill()
  8. >>> a
  9. <__main__.A instance at 0x01F23170>
  10.  
  11. >>> class A():
  12. ... def kill_a(self):
  13. ... print self
  14. ... del self
  15. ... def kill_b(self):
  16. ... del self
  17. ... print self
  18. ...
  19. >>> a = A()
  20. >>> b = A()
  21. >>> a.kill_a()
  22. <__main__.A instance at 0xb771250c>
  23. >>> b.kill_b()
  24. Traceback (most recent call last):
  25. File "<stdin>", line 1, in <module>
  26. File "<stdin>", line 7, in kill_b
  27. UnboundLocalError: local variable 'self' referenced before assignment
  28.  
  29. player.bag.remove(cat)
  30.  
  31. here.add( cat )
  32. player.bag.remove(cat)
  33.  
  34. a = A()
  35. a.kill()
  36.  
  37. class Generic:
  38. ''' Generic class all other classes inherit '''
  39. def kill(self):
  40. del self
  41. #Need a way to remove the instance of the class
  42.  
  43. class Item(Generic):
  44. def __init__(self,name):
  45. self.name = name
  46.  
  47. class Being(Generic):
  48. def __init__(self,name):
  49. self.name = name
  50. self.bag = []
  51.  
  52. def pick_up(self,target):
  53. self.bag.append(target)
  54. target.kill()
  55.  
  56. player = Being('Hero')
  57. print player
  58.  
  59. cat = Item('Cat')
  60. print cat
  61.  
  62.  
  63. print'Players inventory',player.bag
  64.  
  65. print'Picking up the cat'
  66. player.pick_up(cat)
  67.  
  68. print'Players inventory',player.bag
  69.  
  70. class foo(object):
  71. def __init__(self):
  72. self.some_big_object = some_resource
  73.  
  74. def killBigObject(self):
  75. del some_big_object
  76.  
  77. >>> class manager(object):
  78. ... def __init__(self):
  79. ... self.lookup = {}
  80. ... def addItem(self, name, item):
  81. ... self.lookup[name] = item
  82. ... item.setLookup(self.lookup)
  83. >>> class Item(object):
  84. ... def __init__(self, name):
  85. ... self.name = name
  86. ... def setLookup(self, lookup):
  87. ... self.lookup = lookup
  88. ... def deleteSelf(self):
  89. ... del self.lookup[self.name]
  90. >>> man = manager()
  91. >>> item = Item("foo")
  92. >>> man.addItem("foo", item)
  93. >>> man.lookup
  94. {'foo': <__main__.Item object at 0x81b50>}
  95. >>> item.deleteSelf()
  96. >>> man.lookup
  97. {}
  98.  
  99. def kill_self(exit_msg = 'killed'):
  100. global kill_self
  101. del kill_self
  102. return exit_msg
  103.  
  104. >>> kill_self
  105. <function kill_self at 0x02A2C780>
  106. >>> kill_self()
  107. 'killed'
  108. >>> kill_self
  109. Traceback (most recent call last):
  110. File "<pyshell#28>", line 1, in <module>
  111. kill_self
  112. NameError: name 'kill_self' is not defined
  113.  
  114. >>> x = kill_self
  115. >>> kill_self()
  116. >>> kill_self
  117. NameError: name 'kill_self' is not defined
  118. >>> x
  119. <function kill_self at 0x...>
  120. >>> x()
  121. NameError: global name 'kill_self' is not defined
Add Comment
Please, Sign In to add comment