hash函数定义如下,它的作用是计算对象的哈希值,返回一个整型数字,一般用于字典类型key-value键值对查找。只有不可变对象才能计算哈希值。

hash(object)

Return the hash value of the object (if it has one). Hash values are integers. They are used to quickly compare dictionary keys during a dictionary lookup. Numeric values that compare equal have the same hash value (even if they are of different types, as is the case for 1 and 1.0).

md5是一种摘要算法,它是众多摘要算法中的一种,它的作用是利用md5函数计算对象的摘要,返回的内容是一个32位长度的字符串,这和hash函数有本质区别,一般用来防篡改。当然,也只有不可变对象才能计算摘要值。

Python里面任何一个对象都有type、value、id三个属性,但不一定有hash值。

之前,我把hash函数和md5摘要算法两个概念混为一谈了,今天重新将其理清楚。

参考资料:

results matching ""

    No results matching ""