https://stackoverflow.com/questions/34402522/difference-between-hash-and-id

There are three concepts to grasp when trying to understand idhash and the == and isoperators: identityvalue and hash value. Not all objects have all three.

  1. 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. The is operator compares items by identity, a is b is equivalent to id(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).

  2. 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 time a == b, you can say that a and b 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, 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 from object 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.

  3. 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 function hash(a) returns the object a'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()的更多相关文章

  1. [python数据结构] hashable, list, tuple, set, frozenset

    学习 cs212 unit4 时遇到了 tuple, list, set 同时使用的问题,并且进行了拼接.合并操作.于是我就被弄混了.所以在这里进行一下总结. hashable and unhasha ...

  2. onhashchange事件,只需要修改hash值即可响应onhashchange事件中的函数(适用于上一题下一题和跳转页面等功能)

    使用实例: 使用onhashchange事件做一个简单的上一页下一页功能,并且当刷新页面时停留在当前页 html: <!DOCTYPE html><html><body& ...

  3. Hash表——The Hash table

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include "list.h&q ...

  4. 一个简单的样例看明确怎样利用window.location.hash实现ajax操作时浏览器的前进/后退功能

    我们知道JavaScript中非常早就提供了window.history对象,利用history对象的forward().go().back()方法可以方便实现不同页面之间的前进.后退等这样的导航功能 ...

  5. POJ1200(hash)

    Crazy Search Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27536   Accepted: 7692 Des ...

  6. 百度资深架构师带你深入浅出一致性Hash原理

    一.前言 在解决分布式系统中负载均衡的问题时候可以使用Hash算法让固定的一部分请求落到同一台服务器上,这样每台服务器固定处理一部分请求(并维护这些请求的信息),起到负载均衡的作用. 但是普通的余数h ...

  7. 5-15 QQ帐户的申请与登陆 (25分) HASH

    实现QQ新帐户申请和老帐户登陆的简化版功能.最大挑战是:据说现在的QQ号码已经有10位数了. 输入格式: 输入首先给出一个正整数NN(\le 10^5≤10​5​​),随后给出NN行指令.每行指令的格 ...

  8. codeforces794D dfs+图上hash

    http://codeforces.com/problemset/problem/794/D 题意:在一个国家有 n 座城市和一些双向边.这些城市被编号为 1 到 n. 一共有 m 条双线边,第 i条 ...

  9. Python hash() 函数

    Python hash() 函数  Python 内置函数 描述 hash() 用于获取取一个对象(字符串或者数值等)的哈希值. 语法 hash 语法: hash(object) 参数说明: obje ...

随机推荐

  1. Redis的主从复制(十一)

    1>什么是主从复制 持久化保证了即使redis服务重启也不会丢失数据,因为redis服务重启后(在使用aof和rdb方式时,如果redis重启,则数据从aof文件加载)会将硬盘上持久化的数据恢复 ...

  2. 停止node进程和查看react-native-cli

    taskkill /f /t /im node.exe which react-native

  3. linux ps 按进程消耗内存资源大小排序

    linux ps 关于sort的解释 --sort spec specify sorting order. Sorting syntax is [+|-]key[,[+|-]key[,...]] Ch ...

  4. MySQL_优化

    MySQL优化(http://www.cnblogs.com/zengkefu/p/5683438.html) 1.explain+索引. 在你要查询的语句前加explain,看下有没有用到索引,如果 ...

  5. PHP 多维数组排序 函数怎么保持数字键不被重新索引

    /** * 根据数组中的某个键值大小进行排序,仅支持二维数组 * * @param array $array 排序数组 * @param string $key 键值 * @param bool $a ...

  6. springmvc 在非controller下使用@autowired

    在SpringMVC框架中,我们经常要使用@Autowired注解注入Service或者Mapper接口,我们也知道,在controller层中注入service接口,在service层中注入其它的s ...

  7. C# DES加密类,16位的加密。

    这个加密类是与java写的DES加密不同时,自己写的,最后与Java的加密相同了,解决了加密后不同的问题. 可以直接调用里面的加密和解密的方法. using System; using System. ...

  8. Exp2 后门原理与实践 毛瀚逸 20164318

    Exp2 后门原理与实践 20164318 毛瀚逸 一.实验内容 基础问题回答: 1.例举你能想到的一个后门进入到你系统中的可能方式? 答:下载奇怪的文件并运行,通过操作系统的漏洞来获取电脑的高级权限 ...

  9. 使用python读取MS-SQL数据库

    使用python读取MS-SQL中的数据,这里使用到模板pymssql. 因为不是python自带的模板,所以首先需要使用pip安装,对应命令:pip install pymssql 建立main.p ...

  10. 引擎设计跟踪(九.14.3.4) mile stone 2 - model和fbx导入的补漏

    之前milestone2已经做完的工作, 现在趁有时间记下笔记. 1.设计 这里是指兼容3ds max导出/fbx格式转换等等一系列工作的设计. 最开始, Blade的3dsmax导出插件, 全部代码 ...