Мне удалось воспроизвести это на Python 3.4 и 3.7.
Рассмотрим:
class Comparable:
def _key(self):
raise NotImplementedError
def __hash__(self):
return hash(self._key())
def __eq__(self, other):
...
def __lt__(self, other):
...
class A(Comparable): pass
class B(A):
def __str__(self):
return "d"
def __eq__(self, other):
return isinstance(self, type(other))
def _key(self):
return str(self),
b = B()
Очевидно, можно ожидать, что здесь будет определено b.__hash__
, так как оно определено в Comparable
, который B
является подклассом.
И вот, он определен, но оценивается как None
.Что дает?
>> b
<__main__.B object at 0x00000183C9734978>
>> '__hash__' in dir(b)
True
>> b.__hash__
>> b.__hash__ is None
True
>> B.__mro__
(<class '__main__.B'>, <class '__main__.A'>, <class '__main__.Comparable'>, <class 'object'>)
>> isinstance(b, Comparable)
True
То же поведение воспроизводится при реализации __init__
как super().__init__()
в Comparable
и A
.