Pthon魔术方法(Magic Methods)-hash
Pthon魔术方法(Magic Methods)-hash
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.hash方法
__hash__:
内建函数hash()调用的返回值,返回一个整数。如果定义这个方法该类的实例就可hash。 __eq__:
对应"=="操作符,判断两个对象内容是否相等,返回bool值。
定义了这个方法,如果不提供"__hash__"方法,那么实例将不可hash了。
"__hash__"方法只是返回一个hash值作为set的key,但是去重还需要"__eq__"来判断两个对象是否相等。
hash值相同,只是hash冲突,不能说明两个对象是相等的。因此一半来说提供"__hash__"方法是为了作为set或者dict的key,如果去重同时要提供"__eq__"方法。
温馨提示:
不可hash对象isinstance(p1,collections.Hashable)一定为False。
去重需要提供"__eq__"方法。
二.案例展示
#!/usr/bin/env python
#_*_conding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie """
设计二维坐标类Point,使其成为可hash类型,并比较2个坐标的实例是否相等。
""" from collections import Hashable class Point:
def __init__(self,x,y):
self.x = x
self.y = y def __hash__(self):
return hash((self.x,self.y)) def __eq__(self, other):
if self is other:
return True
else:
return self.x == other.x and self.y == other.y def __repr__(self):
return "{}:{}".format(self.x,self.y) p1 = Point(10,20)
p2 = Point(10,20) print(hash(p1))
print(hash(p2)) print(p1 is p2)
print(p1 == p2)
print(hex(id(p1)),hex(id(p2)))
print(set((p1,p2)))
print(isinstance(p1,Hashable)) #以上代码执行结果如下:
3713074054217192181
3713074054217192181
False
True
0x137152b7888 0x137152b9348
{10:20}
True
三.小时牛刀
1>.list类实例为什么不可hash?
class list(object):
"""
Built-in mutable sequence. If no argument is given, the constructor creates a new empty list.
The argument must be an iterable if specified.
"""
def append(self, *args, **kwargs): # real signature unknown
""" Append object to the end of the list. """
pass def clear(self, *args, **kwargs): # real signature unknown
""" Remove all items from list. """
pass def copy(self, *args, **kwargs): # real signature unknown
""" Return a shallow copy of the list. """
pass def count(self, *args, **kwargs): # real signature unknown
""" Return number of occurrences of value. """
pass def extend(self, *args, **kwargs): # real signature unknown
""" Extend list by appending elements from the iterable. """
pass def index(self, *args, **kwargs): # real signature unknown
"""
Return first index of value. Raises ValueError if the value is not present.
"""
pass def insert(self, *args, **kwargs): # real signature unknown
""" Insert object before index. """
pass def pop(self, *args, **kwargs): # real signature unknown
"""
Remove and return item at index (default last). Raises IndexError if list is empty or index is out of range.
"""
pass def remove(self, *args, **kwargs): # real signature unknown
"""
Remove first occurrence of value. Raises ValueError if the value is not present.
"""
pass def reverse(self, *args, **kwargs): # real signature unknown
""" Reverse *IN PLACE*. """
pass def sort(self, *args, **kwargs): # real signature unknown
""" Stable sort *IN PLACE*. """
pass def __add__(self, *args, **kwargs): # real signature unknown
""" Return self+value. """
pass def __contains__(self, *args, **kwargs): # real signature unknown
""" Return key in self. """
pass def __delitem__(self, *args, **kwargs): # real signature unknown
""" Delete self[key]. """
pass def __eq__(self, *args, **kwargs): # real signature unknown
""" Return self==value. """
pass def __getattribute__(self, *args, **kwargs): # real signature unknown
""" Return getattr(self, name). """
pass def __getitem__(self, y): # real signature unknown; restored from __doc__
""" x.__getitem__(y) <==> x[y] """
pass def __ge__(self, *args, **kwargs): # real signature unknown
""" Return self>=value. """
pass def __gt__(self, *args, **kwargs): # real signature unknown
""" Return self>value. """
pass def __iadd__(self, *args, **kwargs): # real signature unknown
""" Implement self+=value. """
pass def __imul__(self, *args, **kwargs): # real signature unknown
""" Implement self*=value. """
pass def __init__(self, seq=()): # known special case of list.__init__
"""
Built-in mutable sequence. If no argument is given, the constructor creates a new empty list.
The argument must be an iterable if specified.
# (copied from class doc)
"""
pass def __iter__(self, *args, **kwargs): # real signature unknown
""" Implement iter(self). """
pass def __len__(self, *args, **kwargs): # real signature unknown
""" Return len(self). """
pass def __le__(self, *args, **kwargs): # real signature unknown
""" Return self<=value. """
pass def __lt__(self, *args, **kwargs): # real signature unknown
""" Return self<value. """
pass def __mul__(self, *args, **kwargs): # real signature unknown
""" Return self*value. """
pass @staticmethod # known case of __new__
def __new__(*args, **kwargs): # real signature unknown
""" Create and return a new object. See help(type) for accurate signature. """
pass def __ne__(self, *args, **kwargs): # real signature unknown
""" Return self!=value. """
pass def __repr__(self, *args, **kwargs): # real signature unknown
""" Return repr(self). """
pass def __reversed__(self, *args, **kwargs): # real signature unknown
""" Return a reverse iterator over the list. """
pass def __rmul__(self, *args, **kwargs): # real signature unknown
""" Return value*self. """
pass def __setitem__(self, *args, **kwargs): # real signature unknown
""" Set self[key] to value. """
pass def __sizeof__(self, *args, **kwargs): # real signature unknown
""" Return the size of the list in memory, in bytes. """
pass __hash__ = None
class list(object):(源码解析)

2>.functools.lru_cache使用到的functools.HashedSeq类继承自list,为什么可hash?
class _HashedSeq(list):
""" This class guarantees that hash() will be called no more than once
per element. This is important because the lru_cache() will hash
the key multiple times on a cache miss. """ __slots__ = 'hashvalue' def __init__(self, tup, hash=hash):
self[:] = tup
self.hashvalue = hash(tup) def __hash__(self):
return self.hashvalue
class _HashedSeq(list):源码解析

Pthon魔术方法(Magic Methods)-hash的更多相关文章
- php中的魔术方法(Magic methods)和魔术常亮
PHP中把以两个下划线__开头的方法称为魔术方法,这些方法在PHP中充当了举足轻重的作用. 魔术方法包括: __construct(),类的构造函数 __destruct(),类的析构函数 __cal ...
- Pthon魔术方法(Magic Methods)-描述器
Pthon魔术方法(Magic Methods)-描述器 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.描述器概述 1>.描述器定义 Python中,一个类实现了&quo ...
- Pthon魔术方法(Magic Methods)-反射
Pthon魔术方法(Magic Methods)-反射 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.反射概述 运行时,区别于编译时,指的时程序被加载到内存中执行的时候. 反射 ...
- Pthon魔术方法(Magic Methods)-上下文管理
Pthon魔术方法(Magic Methods)-上下文管理 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.上下文管理方法 __enter__: 进入与此对象相关的上下文.如果 ...
- Pthon魔术方法(Magic Methods)-可调用对象
Pthon魔术方法(Magic Methods)-可调用对象 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.可调用对象方法 __call__: 类中定义一个该方法,实例就可以像 ...
- Pthon魔术方法(Magic Methods)-容器相关方法
Pthon魔术方法(Magic Methods)-容器相关方法 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.容器相关方法汇总 __len__: 内建函数len(),返回对象的 ...
- Pthon魔术方法(Magic Methods)-运算符重载
Pthon魔术方法(Magic Methods)-运算符重载 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Python运算符对应的魔术方法 1>.比较运算符 <: ...
- Pthon魔术方法(Magic Methods)-bool
Pthon魔术方法(Magic Methods)-bool 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.bool方法 __bool__: 内建函数bool(),或者对象放在逻 ...
- Pthon魔术方法(Magic Methods)-可视化
Pthon魔术方法(Magic Methods)-可视化 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.关于可视化的魔术方法简介 __str__: str()函数,format ...
随机推荐
- aliyun手记
阿里云里面购买的带宽是指外网带宽,内网默认是千兆带宽,做过I/O优化的则是万兆带宽. 修改密码实在更多(三个点)的那里进行修改的:修改密码(windows是administrator以及Linux是r ...
- PAT-2019年冬季考试-甲级 7-2 Block Reversing (25分) (链表转置)
7-2 Block Reversing (25分) Given a singly linked list L. Let us consider every K nodes as a block ( ...
- Python 的语言特性
谈谈对 Python 和其他语言的区别 Python 是一门语法简洁优美,功能强大无比,应用领域非常广泛,具有强大完备的第三方库,他是一门强类型的可移植.可扩展,可嵌入的解释型编程语言,属于动态语言. ...
- Google Adsense(谷歌网站联盟)广告申请指南
Google AdSense 是一种获取收入的快速简便的方法,适合于各种规模的网站发布商.它可以在网站的内容网页上展示相关性较高的 Google 广告,并且这些广告不会过分夸张醒目.由于所展示的广告同 ...
- linux 把nginx加入到系统服务的方法
linux 把nginx加入到系统服务的方法一.首先写一个shell脚本,脚本名称:nginx<pre>#! /bin/bash# chkconfig: 35 85 15 # descri ...
- SQlL Server ----- 通过年月进行查询
websit 中的代码. 不修改 对控件进行修改,展示年月 WdatePicker({ dateFmt: 'yyyy-MM', isShowToday: false, isShowClear: fa ...
- 【转帖】Kafka入门介绍
Kafka入门介绍 https://www.cnblogs.com/swordfall/p/8251700.html 最近在看hdoop的hdfs 以及看了下kafka的底层存储,发现分布式的技术基本 ...
- java知识精要(二)
java知识精要(一) 集合 Iterable v.s. Iterator 两者都是接口,在Collection继承的是Iterable. Iterable表达了集合具备迭代访问的能力,而Iterat ...
- linux端口映射
参考文章: http://jingyan.baidu.com/article/ed15cb1b2a332e1be36981ed.html http://www.myhack58.com/Article ...
- JVM与并发
1.jvm内存模型 硬件内存模型 处理器-->高速缓存-->缓存一致性协议-->主存 java内存模型 线程<-->工作内存<-->save和load < ...