On Jan 23, 5:59 pm, [EMAIL PROTECTED] wrote: > I am writing a game, and it must keep a list of objects. I've been > representing this as a list, but I need an object to be able to remove > itself. It doesn't know it's own index. If I tried to make each object > keep track of it's own index, it would be invalidated when any object > with a lower index was deleted. The error was that when I called > list.remove(self), it just removed the first thing in hte list with > the same type as what I wanted, rather than the object I wanted. The > objects have no identifying charachteristics, other than thier > location in memory > > So my question: How do I look something up in a list by it's location > in memory? does python even support pointers? > > Is there a better way?
How about adding an id attribute to your objects, which will contain a unique identifier, override __eq__ to use that id to compare itself to others and then simply pop off the object using object_list.pop(object_list.index(self)). Something like this: >>> class Spam (object) : def __init__ (self, id) : self.id = id def __eq__ (self, other) : try : return self.id == other.id except AttributeError : return False >>> >>> a,b,c = Spam(1), Spam(2), Spam(3) >>> x = [a,b,c] >>> x.pop(x.index(c)) <__main__.Spam object at 0x885e5ac> Except your object would be telling the list to pop self of course, and you'd need someway of insuring the uniqueness of your IDs. -- http://mail.python.org/mailman/listinfo/python-list