mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Classes that override __eq__ also need to define __hash__.
This commit is contained in:
parent
5e0c2748fb
commit
e1d665a90e
2 changed files with 9 additions and 0 deletions
|
@ -93,4 +93,6 @@ def cmp_to_key(mycmp):
|
|||
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
|
||||
|
|
|
@ -345,6 +345,13 @@ class TestCmpToKey(unittest.TestCase):
|
|||
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):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue