0
点赞
收藏
分享

微信扫一扫

Python小记——可散列的数据类型

老榆 2022-06-21 阅读 28
  • 如果一个对象是可散列的,那么在这个对象的生命周期中,它的散列值是不变的。
  • Python里所有的不可变类型都是可散列的(x)。虽然元组本身是不可变序列,它里面的元素可能是其他可变类型的引用。
In [1]: hash([1,2,3])
------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-1-35e31e935e9e> in <module>
----> 1 hash([1,2,3])

TypeError: unhashable type: 'list'

In [2]: hash((1,2,3))
Out[2]: 2528502973977326415

In [3]: hash((1,2,[1,2,3]))
------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-3dc2017fd13d> in <module>
----> 1 hash((1,2,[1,2,3]))

TypeError: unhashable type: 'list'

In [4]: hash((1,2,(1,2,3)))
Out[4]: -3548400018562967053


举报

相关推荐

0 条评论