[Python-checkins] r79808 - in python/trunk/Lib: functools.py test/test_functools.py

raymond.hettinger python-checkins at python.org
Mon Apr 5 20:53:43 CEST 2010


Author: raymond.hettinger
Date: Mon Apr  5 20:53:43 2010
New Revision: 79808

Log:
Classes that override __eq__ also need to define __hash__.

Modified:
   python/trunk/Lib/functools.py
   python/trunk/Lib/test/test_functools.py

Modified: python/trunk/Lib/functools.py
==============================================================================
--- python/trunk/Lib/functools.py	(original)
+++ python/trunk/Lib/functools.py	Mon Apr  5 20:53:43 2010
@@ -93,4 +93,6 @@
             return mycmp(self.obj, other.obj) >= 0
         def __ne__(self, other):
             return mycmp(self.obj, other.obj) != 0
+        def __hash__(self):
+            raise TypeError('hash not implemented')
     return K

Modified: python/trunk/Lib/test/test_functools.py
==============================================================================
--- python/trunk/Lib/test/test_functools.py	(original)
+++ python/trunk/Lib/test/test_functools.py	Mon Apr  5 20:53:43 2010
@@ -345,6 +345,13 @@
         self.assertEqual(sorted(range(5), key=functools.cmp_to_key(mycmp)),
                          [4, 3, 2, 1, 0])
 
+    def test_hash(self):
+        def mycmp(x, y):
+            return y - x
+        key = functools.cmp_to_key(mycmp)
+        k = key(10)
+        self.assertRaises(TypeError, hash(k))
+
 class TestTotalOrdering(unittest.TestCase):
 
     def test_total_ordering_lt(self):


More information about the Python-checkins mailing list