I am curious why you would like to do this. Most likely, you should just let the garbage collection do its job. In python, garbage collection is pretty deterministic. Therefore, you do not need to worry about just leaving the objects that are in memory, as in other languages ββ(not to mention the fact that recalculation has no drawbacks).
Although one thing you should consider is wrapping around any objects or resources that you can get rid of later.
class foo(object): def __init__(self): self.some_big_object = some_resource def killBigObject(self): del some_big_object
In response to Null addendum :
Unfortunately, I do not think that there is a way to do what you want to do the way you want. Here is one way you might want to consider:
>>> class manager(object): ... def __init__(self): ... self.lookup = {} ... def addItem(self, name, item): ... self.lookup[name] = item ... item.setLookup(self.lookup) >>> class Item(object): ... def __init__(self, name): ... self.name = name ... def setLookup(self, lookup): ... self.lookup = lookup ... def deleteSelf(self): ... del self.lookup[self.name] >>> man = manager() >>> item = Item("foo") >>> man.addItem("foo", item) >>> man.lookup {'foo': <__main__.Item object at 0x81b50>} >>> item.deleteSelf() >>> man.lookup {}
This is a bit messy, but it should give you this idea. In fact, I do not think that linking the existence of an element in a game to whether it is a good idea highlighted in memory. This is because the conditions for collecting garbage are likely to differ from the conditions for the item in the game. So you donβt need to worry so much about it.
Jason Baker Nov 16 '08 at 4:19 2008-11-16 04:19
source share