python - 如何使 python 数据类可哈希?

标签 python python-3.x hash python-dataclasses

假设我在 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 and frozen are both true, by default dataclass() will generate a __hash__() method for you. If eq is true and frozen is false, __hash__() will be set to None, marking it unhashable (which it is, since it is mutable). If eq 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/

相关文章:

python-3.x - 有没有办法为 ipywidgets 中的容器设置标题值?

python - 如何将列中的值更改为二进制?

python - Python 中的基本 bool 表达式产生令人惊讶的结果

ruby-on-rails - 在 Ruby 中根据值从哈希集中选择一些哈希

go - 哈希字符串的有限并行性

python - 在 pandas 中对 groupby 内的类别值进行排序

python - 如何让 Celery 从命令行加载配置?

Python 的 pyparsing : Implementing grammar to parse logical AND expression

mysql - MYSQL数据库中的杂字符?

python - 如何编写时间相关的测试 Python