iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python中使用__hash__和__eq__方法的问题
  • 401
分享到

Python中使用__hash__和__eq__方法的问题

2024-04-02 19:04:59 401人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录python使用__hash__和__eq__的问题Python类中特殊方法__eq__和__hash__关系Python使用__hash__和__eq__的问题 代码版本3.6

Python使用__hash__和__eq__的问题

  • 代码版本3.6.3    
  • 文档版本:3.6.6

 object.__hash__(self)

Called by built-in function hash() and for operations on members of hashed collections including set, frozenset, 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类中特殊方法__eq__和__hash__关系

class Point(object):

    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __repr__(self):
        return repr((self.id, self.x, self.y))

    def __eq__(self, other):
        return self.x == other.y and self.y == self.y

    def __hash__(self):
        return hash((self.x, self.y))

上面定义了一个二维点的类其中__repr__主要用来以一个字符串表示该类的实例,例如Point(1,2),在调试时打印该点会获得字符串(1,2)。

当对两个点的实例进行值的比较时,比如p1=Point(1,1) p2=Point(1,2),判断p1==p2时__eq__()会被调用,用以判断两个实例是否相等。在上述代码中定义了只要x和y的坐标相同,两个点相等。需要注意,__eq__()对is不生效,==是比较的值,而is比较的是引用,也就是内存地址。举个例子,p1=Point(1,1) p2=Point(1,1),p1==p2为True,p1 is p2为False,只有p1 is p1为True。

在Python中对象分为可哈希对象和不可哈希对象,可哈希对象如字符串、数字、自定义的类、frozenset、元组,被称作不可变对象,不可哈希对象如字典、列表、集合,被称作可变对象。这里的不可变不是对象的值不可变,而是指对象创建后其hash值在其生命周期内不会改变。用函数hash()取可哈希对象的hash值,只要是同一对象其hash值不会改变;而对不可哈希对象取hash值,例如对列表取hash值,会报错,返回TypeError: unhashable type: 'list'。可哈希对象因其hash值不变可以用作字典的key,而不可哈希对象则不行。

当需要对类的一个实例取其hash值时,会调用__hash__()。一般来说,会把实例的所有属性打包成元组,返回其hash值,从而实现自定义__hash__()。在用set()去重时就是对比hash值是否一样,如果两个对象hash值一样代表重复。

用户定义的类默认带有__eq__()和 __hash__()方法;使用它们与任何对象(自己除外)比较必定不相等,并且 x.__hash__()会返回一个恰当的值以确保 x == y 同时意味着 x is y且 hash(x) == hash(y)。

如果一个类没有定义__eq__()方法,那么也不应该定义 __hash__()操作;如果它定义了__eq__()但没有定义 __hash__(),那么__hash__()会被隐式地设为None,这个类就变成了不可哈希对象。如果一个类定义了可变对象并实现了 __eq__()方法,则不应该实现__hash__(),因为可哈希集的实现要求键的哈希集是不可变的。例如,Point类中添加一个属性li是一个列表,由于列表不可哈希所以强行放入包含属性的元组中并返回其哈希值会报错。



如果使用默认的__hash__()则不论如何改变一个实例的值其hash值都不变;反之,使用本文这种自定义的__hash__()方法,实例的值改变后,hash值就会改变。因此,自定义__hash__()方法的类的实例不应该作为字典的key(强行作为key不会报错,但是改变实例的属性值会导致找不到key对应的value),key的哈希值必须唯一不可变,key的hash值改变会导致找不到key对应的value。






以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: Python中使用__hash__和__eq__方法的问题

本文链接: https://www.lsjlt.com/news/120645.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作