【发布时间】:2016-03-02 21:23:50
【问题描述】:
为什么 Java 中的 ArrayList 是可散列的,而 Python 中的 List 不是。它只是基于开发人员或语言语义的选择。
我认为 Python 不允许列表是可散列的,因为它是可变的,因此散列可以在对象的生命周期内发生变化。
Java 允许它是好特性还是坏特性。
public class Test {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("hello");
System.out.println(list.hashCode());
}
}
工作正常,而下面不行
>>> l = ["hello"]
>>> l.__hash__()
Traceback (most recent call last):
File "<pyshell#103>", line 1, in <module>
l.__hash__()
TypeError: 'NoneType' object is not callable
【问题讨论】:
-
请告诉我们为什么投反对票
-
我尝试编辑您的问题,使其不那么随意和模糊。不要责怪 我 进一步投反对票。