python - 如何在仅包含列表的类上实现散列?
问题描述
我有以下 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。有没有更好的选择?
解决方案
推荐阅读
- php - 内置服务器 PHP 的 Google Cloud Shell 问题 - 无法连接到端口 8080 上的 Cloud Shell
- java - Akka IO 随机重置 TCP 连接
- javascript - 简单的演示项目 Webpack KO(with a components) javascript
- logstash - Grok 问题 - 处理“SOME-TEXT”模式
- wordpress - WordPress 语言取决于地理区域
- html - 如何动态更改Angular模板中的属性名称?
- java - 休眠验证器不适用于 Spring MVC 应用程序
- java - Infinispan 驱逐与树缓存
- memory-management - 紧凑的 trie 有什么好处?
- python - 如何将数组中的各个条目放入具有自己变量的数组中?