Python中字典的key都可以是什么?_Inotime的博客-CSDN博客_python字典的key是什么类型
答:一个对象能不能作为字典的key,就取决于其有没有__hash__方法。所以所有python自带类型中,除了list、dict、set和内部至少带有上述三种类型之一的tuple之外,其余的对象都能当key。
而我们自己定义的类,一般情况下都直接间接的和object有关,都带有__hash__方法。
字典的key不能是可变类型
strings and numbers can always be keys
Tuples can be used as keys if they contain only strings, numbers, or tuples
It is best to think of a dictionary as an unordered set of key: value pairs, with the requirement that the keys are unique (within one dictionary).