On Sun, 27 Mar 2005 18:08:41 -0500, Orri Ganel <[EMAIL PROTECTED]> wrote: > Hello all, > > I am attempting to implement a LinkedList in Python, and in doing so, > have created a Node class which represents the elements of the > LinkedList. However, since these Nodes have custom-defined __lt__(), > __gt__(), __le__(), __ge__(), __eq__(), and __ne__() methods, they > are, for some reason, unhashable. When I comment out these methods, > Python makes no complaint. But when they are present, 'TypeError: > unhashable instance' is raised when attempting to use a Node as a key > in a dictionary.
When you don't define __eq__(), Python will use an object's ID as its hash value. However, the standard usage of hashes implies that objects which compare as equal should hash identically. This means that objects which define __eq__() and which are intended to be hashed need to define a __hash__() method. Be very careful if you're doing this with mutable objects, though -- you need to either hash by object identity or by object equality, and for mutable objects either choice will leave you breaking an implied promise. (Either a single object will not hash the same at different points in its life, or objects which are equal will not hash identically.) Jeff Shannon _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor