博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
实习小记-python中可哈希对象是个啥?what is hashable object in python?
阅读量:4325 次
发布时间:2019-06-06

本文共 3334 字,大约阅读时间需要 11 分钟。

  废话不多说直接祭上python3.3x的文档:()

 object.__hash__(self)

Called by built-in function and for operations on members of hashed collections including , , and . should return an integer. The only required property is that objects which compare equal have the same hash value; it is advised to somehow mix together (e.g. using exclusive or) the hash values for the components of the object that also play a part in comparison of objects.

  可哈希对象是对象拥有__hash__(self)内置函数的对象。对于可哈希的对象执行这个函数将会返回一个整数。可哈希对象判断相等的唯一条件就是两者 的哈希值相等。如果我们的对象有多个属性值,我们会使用一种方法(比方说逻辑运算异或)来将其属性值结合在一起做比较。(如果不对,麻烦一定告诉我,谢谢!)

 If a class does not define an method it should not define a operation either; if it defines but not , its instances will not be usable as items in hashable collections. If a class defines mutable objects and implements an method, it should not implement , 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__,那么它的实例在某个可哈希集合中将会无效(比方说在字典/集合这类类型中)。如果一个类型定义了一个可变对象而且定义了__eq__方法,那么它不应该去定义__hash__方法,因为在哈希集合中要求其中元素的哈希值是不变的(如果某个对象实例的哈希值发生了改变,那么他将会到错误的哈希表中。。)。

User-defined classes have and 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__方法;有了它,所有的对象实例都是不等的(除非是自己和自己比较),在做 x == y 比较时是和这个等价的 hash(x) == hash(y)

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

  这句话的意思是,如果我们定义了一个类型,该类型只定义了__eq__而没有定义__hash__的话,那么他的__hash__()会隐式的设置为None,如果某个情况下需要这个类型的哈希值,那么程序将会报错。具体请看下面代码:

>>> class A:...     def __eq__(self, other):...         return True...     >>> a = A()>>> import collections>>> isinstance(a, collections.Hashable)  # 发现它不是可哈希对象False>>> a.__hash__>>> a.__hash__()Traceback (most recent call last):  File "
", line 1, in
a.__hash__()TypeError: 'NoneType' object is not callable

  然后往下看文档

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

  如果某个重写了__eq__方法的对象需要保留__hash__属性,那么我们需要在类型设置中添加该语句 __hash__ = <ParentClass>.__hash__

请看代码

 

>>> class A:...     __hash__ = object.__hash__...     def __eq__(self, other):...         return True...     >>> a = A()>>> a.__hash__
>>> set([a,2,3]){
<__main__.A object at 0x7f21029cfa10>, 2, 3}

 

 

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

  如果某个类型定义需要将__hash__属性删掉,那么我们可以在类变量中这样写 __hash__ = None

 

  看完了还是有点小激动的,今天因为一个偶然原因,接触到了这么多的python知识。真的是相当高兴阿!

  然而我也发现__eq__ __hash__这两个方法不能随意动,如果我么需要改写其中一个的话一定要仔细考虑可能的情况,以免出现问题。

 

转载于:https://www.cnblogs.com/Blaxon/p/4671267.html

你可能感兴趣的文章
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_1-2.中大型公司里面项目开发流程讲解...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_1-1.SpringBoot整合微信支付开发在线教育视频站点介绍...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-1.快速搭建SpringBoot项目,采用Eclipse...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_1-4.在线教育后台数据库设计...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-3.热部署在Eclipse和IDE里面的使用...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_1-3.在线教育站点需求分析和架构设计...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-4.后端项目分层分包及资源文件处理...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-2.快速搭建SpringBoot项目,采用IDEA...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-7.接口配置文件自动映射到属性和实体类配置...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-5.开源工具的优缺点选择和抽象方法的建议...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_3-1.整合Mybatis访问数据库和阿里巴巴数据源...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-6.Mysql逆向工程效率神器之使用IDE自动生成Java实体类...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_3-4.动态Sql语句Mybaties SqlProvider...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_4-1.单机和分布式应用的登录检验讲解...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_4-3.登录检验JWT实战之封装通用方法...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_3-2.使用Mybatis注解开发视频列表增删改查...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_5-2.微信扫一扫功能开发前期准备...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_3-3.Vidoe相关接口完善和规范协议...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_5-3.微信Oauth2.0交互流程讲解...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_3-5.PageHelper分页插件使用
查看>>