Python中的__hash__和__eq__方法之间的一些使用问题

在Python中,如果自定义类没有定义__hash__()方法,不应定义__eq__()方法,因为这将使实例无法作为哈希集合的元素。如果类实现了可变对象并重写了__eq__(),不应再实现__hash__(),以防止因哈希值变化导致对象放入错误的哈希桶。默认情况下,用户定义的类都有__eq__()和__hash__(),若重写__eq__()而不定义__hash__(),则__hash__会被隐式设为None,调用时会引发TypeError。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 代码版本3.6.3    文档版本:3.6.6

 object.__hash__(self)

Called by built-in function hash() and for operations on members of hashed collections including setfrozenset, and dict.

 __hash__()方法会被上述四种情况调用。

 

 

If a class does not define an __eq__() method it should not define a __hash__()operation either; if it defines __eq__() but not __hash__(), its instances will not be usable as items in hashable collections. If a class defines mutable objects and implements an __eq__() method, it should not implement __hash__(), since the implementation of hashable collections requires that a key’s hash value is immutable (if the object’s hash value changes, it will be in the wrong hash bucket).

如果自定义类没定义__eq__()方法,那也不应该定义__hash__()方法。 

如果定义了__eq__()方法没有定义__hash__()方法,那么它无法作为哈希集合的元素使用(这个hashable collections值得是set、frozenset和dict)。这其实是因为重写__eq__()方法后会默认把__hash__赋为None(文档后面有说),像list一样。 

class A:
    def __eq__(self, other):
        pass


a = A()

print(a.__hash__)  # None
hash(a)


# TypeError: unhashable type: 'A'

还专门说明:如果定义可变对象的类实现了__eq__()方法,就不要再实现__hash__()方法,否则这个对象的hash值发生变化会导致被放在错误的哈希桶中。这个可以用字典试一下,你的键值不在是一一对应的,只要能让这两个方法返回一致的对象都能改动那个本不属于自己的值,这篇文章的第五个例子就是这种情况。

 

 

User-defined classes have __eq__() and __hash__() methods by default; with them, all objects compare unequal (except with themselves) and x.__hash__() returns an appropriate value such that x == y implies both that x is y and hash(x) == hash(y)

用户定义的类默认都有__eq__()和__hash__()方法,这是从object继承的,如果你不重写任何一个,那么对这个类的两个实例x,y来说,x is y ,x == y , hash(x) == hash(y)会同时成立/不成立,即只有在x就是y的时候成立。

 

 

A class that overrides __eq__() and does not define __hash__() will have its __hash__()implicitly set to None. When the __hash__() method of a class is None, instances of the class will raise an appropriate TypeError when a program attempts to retrieve their hash value, and will also be correctly identified as unhashable when checking isinstance(obj, collections.abc.Hashable).

 重写了__eq__()方法的类会隐式的把__hash__赋为None。当获取实例的哈希值即用到了__hash__()方法时(只有上文提到的四种情况会用到这个方法)就会抛出TypeError错误,上文例子演示过了。并且isinstance判断类型也能正确判断。

# 直接安装不成功  pip install collections2 才行
# collections2==0.3.0  A set of improved data types inspired by the standard library's collections module.
import collections


class A:
    def __eq__(self, other):
        pass


class B:
    pass


a = A()
b = B()

print(isinstance(a, collections.abc.Hashable))  # False
print(isinstance(b, collections.abc.Hashable))  # True

 

 

If a class that overrides __eq__() needs to retain the implementation of __hash__() from a parent class, the interpreter must be told this explicitly by setting __hash__ =<ParentClass>.__hash__.

If a class that does not override __eq__() wishes to suppress hash support, it should include __hash__ = None in the class definition. A class which defines its own __hash__() that explicitly raises a TypeError would be incorrectly identified as hashable by an isinstance(obj, collections.abc.Hashable) call.

 如果一个类重写了__eq__()方法还需要能使用父类的__hash__()方法(上文已说默认情况下是被赋值为None了),那就需要明确的说明一下:例class A;如果一个类没有重写__eq__()方法而又需要让__hash__()失效,那就要明确的赋值为None,像list、set等的源码那样。如果你重写了一个会抛出异常的__hash__()方法,虽然使用时会抛出异常,但是类型判断还是会判断为是可哈希的,这是要注意的:例class B。

import collections


class A:
    def __eq__(self, other):
        pass
    __hash__ = object.__hash__


class B:
    def __hash__(self):
        raise TypeError('There is an error!')


a = A()
b = B()

print(isinstance(a, collections.abc.Hashable))
print(isinstance(b, collections.abc.Hashable))
hash(b)


# 结果:
# True
# True
# ...line 12, in __hash__...
# TypeError: There is an error!

文档位置:3.6.6 object.__hash__

Python中,`__hash__()`是一个特殊方法(也称为魔术方法),用于定义对象的哈希值。哈希值是一个唯一的整数,用于在集合(如字典、集合)中快速查找比较对象。 当我们在自定义类中没有定义`__hash__()`方法时,默认情况下,对象的哈希值是根据对象的内存地址计算的,即`id(obj)`。这意味着相同的对象可能具有不同的哈希值,因为它们在内存中具有不同的地址。 如果我们想要自定义一个类的哈希值,可以在类中实现`__hash__()`方法。这个方法应该返回一个整数,用于表示对象的哈希值。 下面是一个简单的示例,展示了自定义类中`__hash__()`方法使用: ```python class Student: def __init__(self, name, age): self.name = name self.age = age def __hash__(self): # 自定义哈希值为年龄的平方 return self.age ** 2 student1 = Student("Alice", 20) student2 = Student("Bob", 25) print(hash(student1)) # 输出:400 print(hash(student2)) # 输出:625 ``` 在上述示例中,我们定义了一个`Student`类,并在其中实现了`__hash__()`方法,计算哈希值为年龄的平方。通过调用`hash()`函数,我们可以获取对象的哈希值并进行打印。 需要注意的是,如果一个类定义了`__hash__()`方法,那么它也必须定义`__eq__()`方法,用于对象的相等性比较。这是因为哈希值只能用于快速查找,而对象的相等性比较需要通过`__eq__()`方法来确定。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值