【发布时间】:2018-12-15 10:53:15
【问题描述】:
我有一个关于 Python 的 hash(self) 函数的问题。
所以在我的方法中,我有以下代码片段
def __init__(self, upper1, lower1, upper2, lower2):
self.phase = 1
self.gammas = frozenset()
self.gammabars = frozenset()
def __hash__(self):
if self.gammas:
return hash(self.gammas)
elif self.gammabars:
return hash(self.gammabars)
所以我想说:
如果 self.gammas 不为空,则返回 self.gammas 或 self.gammabars 等的 hashvalue。
但是如果我现在开始我的程序,我会得到:
TypeError: __hash__ method should return an integer
那么你知道如何解决这个问题吗?
【问题讨论】:
-
请显示运行程序并重现问题所需的所有代码。
-
@KlausD。嗯,问题是,代码很长。