python - 运行时错误: maximum recursion depth exceeded using default hash method

标签 python hash

为什么我得到“return self.__hash__() 当我使用默认哈希方法时,运行时错误:超出最大递归深度?

This causes the error:

def __hash__(self):
    return self.__hash__()

This works:

def __hash__(self):
    return self.name.__hash__()

最佳答案

当然,您会通过从自身调用方法来进入无限递归:

def method_name(self):
    return self.method_name()

您可能想调用基类的方法?

def __hash__(self):
    return super(ClassName, self).__hash__()

关于python - 运行时错误: maximum recursion depth exceeded using default hash method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23289426/

相关文章:

Java: HashMap 中的复合键

python - 当没有数据像素时对图像应用滤镜

ruby - 如何自动命名哈希中的符号?

python - 使用 pandas 读取 csv 文件时需要在路径名前使用 'r'

python - Pandas:将 *multiple* 集列转换为列表列

c - 发生冲突后在哈希表中实现链接时出现段错误

c++ - 在 C++ 中制作迭代器的哈希表

ruby - 根据 Ruby 中该键的值数合并散列中的键值对的最佳方法

python - 在 Python 中如何处理以下情况?

python - 可编写脚本的 HTTP 基准测试(最好在 Python 中使用)