首页 > 解决方案 > 如何在仅包含列表的类上实现散列?

问题描述

我有以下 Python 类:

class Test:
    def __init__(self, ints: List[int]) -> None:
        self.ints = ints
    def __eq__(self, other: object) -> bool:
        if not isinstance(other, Test):
            return NotImplemented
        return self.ints == other.ints

因为我已经覆盖了equals方法,所以我应该实现hash方法。但是,列表在 Python 中是不可散列的。

我已经读过我可以将其转换List为 aTuple并对其进行哈希处理,但这感觉有点 hacky。有没有更好的选择?

标签: python

解决方案


推荐阅读