Difference between hash() and id()
https://stackoverflow.com/questions/34402522/difference-between-hash-and-id
There are three concepts to grasp when trying to understand id, hash and the == and isoperators: identity, value and hash value. Not all objects have all three.
All objects have an identity, though even this can be a little slippery in some cases. The
idfunction returns a number corresponding to an object's identity (in cpython, it returns the memory address of the object, but other interpreters may return something else). If two objects (that exist at the same time) have the same identity, they're actually two references to the same object. Theisoperator compares items by identity,a is bis equivalent toid(a) == id(b).Identity can get a little confusing when you deal with objects that are cached somewhere in their implementation. For instance, the objects for small integers and strings in cpython are not remade each time they're used. Instead, existing objects are returned any time they're needed. You should not rely on this in your code though, because it's an implementation detail of cpython (other interpreters may do it differently or not at all).
All objects also have a value, though this is a bit more complicated. Some objects do not have a meaningful value other than their identity (so value an identity may be synonymous, in some cases). Value can be defined as what the
==operator compares, so any timea == b, you can say thataandbhave the same value. Container objects (like lists) have a value that is defined by their contents, while some other kinds of objects will have values based on their attributes. Objects of different types can sometimes have the same values, as with numbers:0 == 0.0 == 0j == decimal.Decimal("0") == fractions.Fraction(0) == False(yep,bools are numbers in Python, for historic reasons).If a class doesn't define an
__eq__method (to implement the==operator), it will inherit the default version fromobjectand its instances will be compared solely by their identities. This is appropriate when otherwise identical instances may have important semantic differences. For instance, two different sockets connected to the same port of the same host need to be treated differently if one is fetching an HTML webpage and the other is getting an image linked from that page, so they don't have the same value.In addition to a value, some objects have a hash value, which means they can be used as dictionary keys (and stored in
sets). The functionhash(a)returns the objecta's hash value, a number based on the object's value. The hash of an object must remain the same for the lifetime of the object, so it only makes sense for an object to be hashable if its value is immutable (either because it's based on the object's identity, or because it's based on contents of the object that are themselves immutable).Multiple different objects may have the same hash value, though well designed hash functions will avoid this as much as possible. Storing objects with the same hash in a dictionary is much less efficient than storing objects with distinct hashes (each hash collision requires more work). Objects are hashable by default (since their default value is their identity, which is immutable, in Python 2.7-3.6
hash(x)==id(x)/16). If you write an__eq__method in a custom class, Python will disable this default hash implementation, since your__eq__function will define a new meaning of value for its instances. You'll need to write a__hash__method as well, if you want your class to still be hashable. If you inherit from a hashable class but don't want to be hashable yourself, you can set__hash__ = Nonein the class body.
Difference between hash() and id()的更多相关文章
- [python数据结构] hashable, list, tuple, set, frozenset
学习 cs212 unit4 时遇到了 tuple, list, set 同时使用的问题,并且进行了拼接.合并操作.于是我就被弄混了.所以在这里进行一下总结. hashable and unhasha ...
- onhashchange事件,只需要修改hash值即可响应onhashchange事件中的函数(适用于上一题下一题和跳转页面等功能)
使用实例: 使用onhashchange事件做一个简单的上一页下一页功能,并且当刷新页面时停留在当前页 html: <!DOCTYPE html><html><body& ...
- Hash表——The Hash table
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "list.h&q ...
- 一个简单的样例看明确怎样利用window.location.hash实现ajax操作时浏览器的前进/后退功能
我们知道JavaScript中非常早就提供了window.history对象,利用history对象的forward().go().back()方法可以方便实现不同页面之间的前进.后退等这样的导航功能 ...
- POJ1200(hash)
Crazy Search Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 27536 Accepted: 7692 Des ...
- 百度资深架构师带你深入浅出一致性Hash原理
一.前言 在解决分布式系统中负载均衡的问题时候可以使用Hash算法让固定的一部分请求落到同一台服务器上,这样每台服务器固定处理一部分请求(并维护这些请求的信息),起到负载均衡的作用. 但是普通的余数h ...
- 5-15 QQ帐户的申请与登陆 (25分) HASH
实现QQ新帐户申请和老帐户登陆的简化版功能.最大挑战是:据说现在的QQ号码已经有10位数了. 输入格式: 输入首先给出一个正整数NN(\le 10^5≤105),随后给出NN行指令.每行指令的格 ...
- codeforces794D dfs+图上hash
http://codeforces.com/problemset/problem/794/D 题意:在一个国家有 n 座城市和一些双向边.这些城市被编号为 1 到 n. 一共有 m 条双线边,第 i条 ...
- Python hash() 函数
Python hash() 函数 Python 内置函数 描述 hash() 用于获取取一个对象(字符串或者数值等)的哈希值. 语法 hash 语法: hash(object) 参数说明: obje ...
随机推荐
- python+flask+session写供前端使用的后台接口,实现登录保存session时报错。
RuntimeError: The session is unavailable because no secret key was set. Set the secret_key on the a ...
- redis命令Map类型(五)
如果存储一个对象 这个时候使用String 类型就不适合了,如果在String中修改一个数据的话,这就感到烦琐. hash 散列类型 ,他提供了字段与字段值的映射,当时字段值只能是字符串类型 命令: ...
- WEB学习笔记14-HTML5新特性的使用
(1)定义文档类型声明 <!DOCTYPE html> (2)定义页面编码 <!—HTML 4.01中定义的设置页面编码的方式--> <meta http-equiv=& ...
- 安装mingw编译器
1.下载地址:https://sourceforge.net/projects/mingw/files/latest/download?source=top3_dlp_t5 2. 安装下载的(ming ...
- canvas绘制随机验证码
效果图: 思路: 1, 绘制canvas画布,进行基础设置 2.绘制一个矩形 3.设置验证码的随机数 4.设置验证码随机数的随机颜色 5.绘制随机干扰线 6,绘制随机干扰点 经过以上六个步骤,验证码的 ...
- 在Netbeans的项目中添加JDBC驱动程序
想要使用Java连接MySQL数据库,必须在环境中加入配置JDBC驱动程序 Netbeans中添加JDBC的过程是: 1.首先需要下载JDBC 在百度中搜索“mysqljdbc驱动下载”,找到官网 或 ...
- java数组的for遍历
class ArrayDome { public static void main(String[] args) { int[] arr = {12,51,12,11}; //顺序遍历 for(int ...
- MongoDB慢查询性能分析
最近,长期运营后的港台服出现一个问题,web充值很慢,用gm指令查询玩家信息也很慢.最后定位到MongoDB查询也很慢. 刚开始定位的时候,运营SA直接查指定的玩家,并反映很慢,就猜测是索引的问题 ...
- js排序的方法
//排序算法 window.onload = function(){ var array = [0,1,2,44,4, 324,5,65,6,6, ...
- Address already in use: JVM_Bind:80 异常的解决办法
java.net.BindException: Address already in use: JVM_Bind:80 异常的解决办法 今天遇见了这个端口被占用问题 然后各种百度 先是说 用命令 ne ...