Removing objects

Robert Kern robert.kern at gmail.com
Wed Jan 23 14:44:39 EST 2008


bladedpenguin at gmail.com wrote:

> So, in general, is it more efficient to use a dictionary or to
> override the __eq__ function?

Sorry, I guess I wasn't as clear as I could be. If your classes have not 
overridden __eq__ or __cmp__, list.remove() should have worked just fine. If you 
want your objects to be compared by identity (in list.remove(), as dictionary 
keys, in explicit "x==y" expressions), do not implement __eq__.

Once you've resolved your __eq__ problems, then yes, you should consider a 
dictionary or a set, particularly if your list gets large. By default, if your 
classes have not overridden the __hash__ method, your objects will use identity 
to distinguish themselves.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list