Python @dataclass unhashable type
在使用类装饰器 @dataclass 的时候,碰到无法hash
自定义hash函数:
@dataclass
class Centroids:
xids: np.ndarray
def __hash__(self):
x = self.xids
return hash(x.data.tobytes())
def __eq__(self, other):
return hash(self) == hash(other)
ref:
https://stackoverflow.com/questions/16589791/most-efficient-property-to-hash-for-numpy-array