I've come across an example of weird behavior when transitioning some code from python 2 to python 3. Below's a minimal (?) example of it:
class Bar(object):
def __init__(self, x):
self.x = x
def __eq__(self, other):
return self.x == other.x
b = Bar(1)
print(hash(b))
when run with python2
, this code produces some output (a hash of Bar(1)
), while python3
causes a TypeError: unhashable type: 'Bar'
this means that __hash__
is somehow inherited (from object
?) in python 2.
So, my questions are: what is the hash of Bar(1)
in python 2? And why is the behaviour different?