假设我在 python3 中有一个数据类。我希望能够对这些对象进行哈希处理和排序。
我只希望它们按 id 排序/散列。
我在文档中看到我可以实现 _哈希_ 和所有这些,但我想让数据计算为我完成工作,因为它们旨在处理这个问题。
from dataclasses import dataclass, field
@dataclass(eq=True, order=True)
class Category:
id: str = field(compare=True)
name: str = field(default="set this in post_init", compare=False)
a = sorted(list(set([ Category(id='x'), Category(id='y')])))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'Category'
最佳答案
来自 the docs :
Here are the rules governing implicit creation of a
__hash__()
method:[...]
If
eq
andfrozen
are both true, by defaultdataclass()
will generate a__hash__()
method for you. Ifeq
is true andfrozen
is false,__hash__()
will be set toNone
, marking it unhashable (which it is, since it is mutable). Ifeq
is false,__hash__()
will be left untouched meaning the__hash__()
method of the superclass will be used (if the superclass is object, this means it will fall back to id-based hashing).
由于您设置了 eq=True
并将 frozen
保留为默认值 (False
),因此您的数据类不可散列。
您有 3 个选择:
- 设置
frozen=True
(除了eq=True
),这将使您的类不可变且可散列。 设置
unsafe_hash=True
,这将创建一个__hash__
方法,但会使您的类保持可变,因此如果您的类的实例在存储时被修改,则可能会出现问题在字典或集合中:cat = Category('foo', 'bar') categories = {cat} cat.id = 'baz' print(cat in categories) # False
- 手动实现一个
__hash__
方法。
关于python - 如何使 python 数据类可哈希?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52390576/