python中可哈希是什么意思_实习小记-python中可哈希对象是个啥?what is hashable object in python?...
废话不多说直接祭上python3.3x的文档:(原文链接)
object.__hash__(self)
Called by built-in function hash() and for operations on members of hashed collections including set, frozenset, and dict. __hash__() 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 __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__,那么它的实例在某个可哈希集合中将会无效(比方说在字典/集合这类类型中)。如果一个类型定义了一个可变对象而且定义了__eq__方法,那么它不应该去定义__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__方法;有了它,所有的对象实例都是不等的(除非是自己和自己比较),在做 x == y比较时是和这个等价的 hash(x) == hash(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.Hashable).
这句话的意思是,如果我们定义了一个类型,该类型只定义了__eq__而没有定义__hash__的话,那么他的__hash__()会隐式的设置为None,如果某个情况下需要这个类型的哈希值,那么程序将会报错。具体请看下面代码:
>>> classA:
...def __eq__(self, other):
...returnTrue
...>>> a =A()>>> importcollections>>> 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 __eq__() needs to retain the implementation of __hash__() from a parent class, the interpreter must be told this explicitly by setting __hash__ = .__hash__.
如果某个重写了__eq__方法的对象需要保留__hash__属性,那么我们需要在类型设置中添加该语句 __hash__ = .__hash__
请看代码
>>> classA:
...__hash__ = object.__hash__...def __eq__(self, other):
...returnTrue
...>>> a =A()>>> a.__hash__
>>> set([a,2,3])
{<__main__.a object at>, 2, 3}
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.Hashable) call.
如果某个类型定义需要将__hash__属性删掉,那么我们可以在类变量中这样写 __hash__ = None
看完了还是有点小激动的,今天因为一个偶然原因,接触到了这么多的python知识。真的是相当高兴阿!
然而我也发现__eq__ __hash__这两个方法不能随意动,如果我么需要改写其中一个的话一定要仔细考虑可能的情况,以免出现问题。
python中可哈希是什么意思_实习小记-python中可哈希对象是个啥?what is hashable object in python?...相关推荐
- python读取视频里面爱豆出现的时间_计算任意视频中各人物的出镜时间(附Python实现)...
编译:Bing编者按:本文作者Pulkit Sharma分享了一篇有趣的项目,以<猫和老鼠>为例,计算任意视频中汤姆猫和杰瑞鼠的出镜时长.这一模型也可以用于其他电影,轻松统计各演员的上镜时 ...
- bat从数组中找出相同数字并删除_全网把Map中的hash()分析的最透彻的文章,别无二家...
原文地址:https://mp.weixin.qq.com/s/qCHkzs4JPOipB-ZzqrfbeQ 作者: Hollis 你知道HashMap中hash方法的具体实现吗? 你知道HashTa ...
- 布谷鸟哈希函数的参数_系统学习hash算法(哈希算法)
系统学习hash算法(哈希算法) 转载请说明出处. 前言: 关于本文<系统学习hash算法>的由来.在看到了<十一.从头到尾彻底解析Hash 表算法>这篇文章之后,原文中没有暴 ...
- excelexportentity中设置null不显示的方法_学习笔记-Java中的$符
JavaEE中$符号出现在两个地方,一个是前端jQuery中的$(),一个是后端JSP中的EL表达式${}. 一.$()中放不同的东西代表不同的含义,(1)$(function(){}),表示文档加载 ...
- cad中填充的剖面线不能被修剪_在模具行业中CAD二维制图的相关绘图规范
规范,图纸画得更漂亮,与同事之间交流更顺畅等等,小编将收到的二维图常见的问题整理一下,以供作者参考和学习!达到事半功倍的效率! 除了上述最基本的两个要求(所有线都用黑白色,图层只有2个,1个线宽为0. ...
- Java main方法_解释Java中的main方法,及其作用_一个java文件中可包含多个main方法
public static void main(String[] args) {} 或者 public static void main(String args[]) {} main方法是我们学习Ja ...
- 初中python编程教学的困难与解决研究_初中信息技术课程中存在的问题与解决方案研究...
初中信息技术课程中存在的问题与解决方案研究 初中信息技术课程中存在的问题与解决方案研究 张春秀 (宁夏中卫市海原县三河中学) 摘 要:针对初中信息技术课程的教学特点展开分析,找出初中信息技术课程中存在 ...
- python使用tkinter可以在多个操作系统_在tkinter GUI中使用多个窗口
我正在用python构建一个非常基础的电影推荐GUI,并且我试图在选择一个流派时让它打开一个新窗口.我可以打开窗口,但我有困难分配我的单选按钮到一个新的类.我希望能够选择一个流派,点击下一步,然后根据 ...
- python给定dna等分成两个序列_分析DNA序列中的串联重复序列
解决问题的一个好方法是使用regex.正则表达式是编程中解析strings的常用方法. 使用regex,您可以定义要在字符串中搜索的模式(就像您所做的那样),这是问题的核心. 这意味着regex有自己 ...
最新文章
- Namespace(命名空间)的使用
- 在Linux上限制远程登陆的IP
- k8s 去除master节点污点NoSchedule,添加master节点 尽量不调度
- 【面试虐菜】—— JAVA面试题(2)
- 学习 Message(14): 区分左右 Shift、Ctrl、Alt
- JavaI/O:使用InputStreamReader和BufferedReade实现控制器输入输出
- 在PHP里使用 ImageMagick 生成 base64 图片
- Delphi V7.0企业汉化版及注册码
- 建造者模式(Builder Pattern)(转自TerryLee)
- Git .gitignore文件忽略(自己做过测试了)
- 思科设备snmp配置。
- Building a Space Station--POJ 2031
- 【转】算法导论学习笔记 一 分治算法
- 《那些年啊,那些事——一个程序员的奋斗史》——63
- python在线搭建教程_理解python web开发,轻松搭建web app!
- 电脑大小写怎么切换_苹果笔记本电脑pro系列键盘的应用攻略
- C++基础::非类型模板参数在STL中的应用
- java课程设计实验报告_《java课程设计实验报告.doc
- 巧用电视卡让笔记本屏幕作PS2游戏显示器
- 获取微信公众号地址的图片不能正常显示的问题
热门文章
- Amazon CloudFront CDN + s3 源站跨域配置
- python广深地区房价数据的爬取与分析
- icap协议————2014.6.4
- Invariance Matters: Exemplar Memory for Domain Adaptive Person Re-identification
- 以WhatsApp为例,外贸人如何从0到1构建社交营销闭环全流程
- RHEL下编译wireshark源码
- 海龟交易法则13_系统
- 超灵敏磁传感解决方案助力汽车产业发展
- 浅谈spring之IoC控制反转
- 解决Linux上WPS字体缺失问题