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 is
operators: 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
id
function 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. Theis
operator compares items by identity,a is b
is 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 thata
andb
have 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,bool
s 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 fromobject
and 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
set
s). 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__ = None
in 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 ...
随机推荐
- 3D数学基础(三)矩阵
3D引擎中对于矩阵的使用非常多,介绍这些知识也是为了告诉开发者原理,更有助于开发者编写逻辑. (1)固定流水线 各种坐标系之间的转化是通过矩阵相乘得到的,这里面就涉及到了3D固定流水线.作为3D游戏开 ...
- Java中语法与C/CPP的区别
static不能在成员方法中定义,只能作为类变量定义.
- 大硬盘(大于2T)分区方法
背景 在使用fdisk建立分区时,我们最大只能建立2TB大小的分区.如需建立超过2T的分区需要采用GPT磁盘模式.下文补充一下GPT和MBR的基础知识和分超过2T分区的方法. 基本概念 MBR 1.M ...
- C#引用C++的DLL方案(C#调用非托管动态链接库)
SocketClientInit是C++里面定义的方法,通过EntryPoint = "?SocketClientInit@@YAHHHPAD@Z"指出这个函数的真正入口处,方法是 ...
- ReentrantLock+线程池+同步+线程锁
1.并发编程三要素? 1)原子性 原子性指的是一个或者多个操作,要么全部执行并且在执行的过程中不被其他操作打断,要么就全部都不执行. 2)可见性 可见性指多个线程操作一个共享变量时,其中一个线程对变量 ...
- postman设置环境变量
postman属于一键式安装,不多赘述 1.设置环境变量 点击设置进入 添加环境变量 添加成功可选择 应用{{}}包住变量名即可 地址变化更换即可
- ANOVA-方差分析和单尾方差分析
https://www.cnblogs.com/webRobot/p/6877283.html https://blog.csdn.net/zijinmu69/article/details/8056 ...
- MVC中调用模态框之后导致JS失效
今天在工作中碰到一个页面调用模态框之后,页面原来的JS失效的问题,由于前台经验较少,折腾了一天... 问题描述是这样,在页面,有两个下拉列表框A和B,做了下拉列表框联动,有一个button按钮会调用模 ...
- Java面试3
反射的定义: 反射是java语言的一个特性,它允程序在运行时(注意不是编译的时候)来进行自我检查并且对内部的成员进行操作.例如它允许一个java的类获取它所有的成员变量和方法并且显示出来. 反射机制的 ...
- java项目---用java实现简单TCP服务器监听(3星)
---------------------------------------------服务端----------------------------------------------- 1 pa ...