Types that define `__eq__` are unhashable?

后端 未结 4 1671
青春惊慌失措
青春惊慌失措 2020-11-27 05:05

I had a strange bug when porting a feature to the Python 3.1 fork of my program. I narrowed it down to the following hypothesis:

In contrast to Python 2.x, in Python

4条回答
  •  佛祖请我去吃肉
    2020-11-27 05:32

    This paragraph from http://docs.python.org/3.1/reference/datamodel.html#object.hash

    If a class that overrides __eq__() needs to retain the implementation of __hash__() from a parent class, the interpreter must be told this explicitly by setting __hash__ = .__hash__. Otherwise the inheritance of __hash__() will be blocked, just as if __hash__ had been explicitly set to None.

提交回复
热议问题