hash() 函数可以应用于数字, 字符串和对象, 不能直接应用于 list,set,dictionary.
在 hash() 对对象使用时, 所得的结果不仅和对象的内容有关, 还和对象的 id(), 也就是内存地址有关.
- class Test:
- def __init__(self, i):
- self.i = i
- for i in range(10):
- t = Test(1)
- print(hash(t), id(t))
输出结果:
- (277855628, 4445690048)
- (277855637, 4445690192)
- (277855628, 4445690048)
- (277855637, 4445690192)
- (277855628, 4445690048)
- (277855637, 4445690192)
- (277855628, 4445690048)
- (277855637, 4445690192)
- (277855628, 4445690048)
- (277855637, 4445690192)
忘忧北萱草
忘忧北萱草
wyb***qq.com
11 个月前 (04-06)
来源: http://www.bubuko.com/infodetail-2976651.html