python可哈希数据类型_如何使python数据类可哈希化?

Say a I have a dataclass in python3. I want to be able to hash and order these objects.

I only want them ordered/hashed on id.

I see in the docs that I can just implement __hash__ and all that but I'd like to get datacalsses to do the work for me because they are intended to handle this.

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 "", line 1, in

TypeError: unhashable type: 'Category'

解决方案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).

Since you set eq=True and left frozen at the default (False), your dataclass is unhashable.

You have 3 options:

Set frozen=True (in addition to eq=True), which will make your class immutable and hashable.

Set unsafe_hash=True, which will create a __hash__ method but leave your class mutable, thus risking problems if an instance of your class is modified while stored in a dict or set:

cat = Category('foo', 'bar')

categories = {cat}

cat.id = 'baz'

print(cat in categories) # False

Manually implement a __hash__ method.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值