4

Why is ArrayList in Java hashable and List in Python not. It's just based on the choice of the developers or language semantics.

I think Python doesn't allow list to be hashable because it's mutable and hence the hash can change over the lifetime of the object.

It's a good feature or bad feature why does Java allow it.

public class Test {

    public static void main(String[] args) {


        ArrayList<String> list = new ArrayList<String>();

        list.add("hello");

        System.out.println(list.hashCode());

    }

}

works fine while below doesn't

>>> l = ["hello"]
>>> l.__hash__()

Traceback (most recent call last):
  File "<pyshell#103>", line 1, in <module>
    l.__hash__()
TypeError: 'NoneType' object is not callable
User
  • 483
  • 4
  • 19

1 Answers1

4

You are correct that Lists in Python are not hashable because Python does not permit mutable data to be keys to dictionaries.

Java ArrayLists are hashable because Java, one, does not have any language-level support for immutability, and two forces all objects to be hashable. Whether the hash is "useful" depends on the class's implementation.

I think the "story" is simply that Python has language-level support for dictionaries and has made some design decisions around this, while Java does not.

While Python is "weakly typed" for some definition of "weakly," you should not infer that Python is the Wild West of programming languages where everything goes with no built-in safety. That would be Perl or Javascript.

djechlin
  • 59,258
  • 35
  • 162
  • 290
  • I don't understand what you mean by language level support, dictionaries are there in Java also. – User Nov 29 '15 at 01:20
  • @User is it a java 8 feature? if so then change my answer to say "Through Java 7," if not then no there isn't. – djechlin Nov 29 '15 at 01:22
  • 1
    Python is strongly typed. – User Nov 29 '15 at 01:24
  • 1
    @User http://ericlippert.com/2012/10/15/is-c-a-strongly-typed-or-a-weakly-typed-language/ – djechlin Nov 29 '15 at 01:25
  • 2
    @User dictionaries are part of the language in Python (e.g. kwargs). Java has `HashMap` and `Hashtable`, but these are just classes. There is no special syntax or language feature which requires them. – Paul Boddington Nov 29 '15 at 01:27
  • @djechlin I liked the C# post but Python is considered strongly typed, can you help with example of weakly typed - http://stackoverflow.com/questions/11328920/is-python-strongly-typed – User Nov 29 '15 at 01:35
  • @User "“Weakly typed” means “this language uses a type verification system that I find distasteful“, and “strongly typed” means “this language uses a type system that I find attractive“." -- that post – djechlin Nov 29 '15 at 01:38
  • @User "These terms are meaningless and you should avoid them. Wikipedia lists eleven different meanings for “strongly typed”, several of which contradict each other. Any time two people use “strongly typed” or “weakly typed” in a conversation about programming languages, odds are good that they have two subtly or grossly different meanings in their heads for those terms, and are therefore automatically talking past each other." -- same post – djechlin Nov 29 '15 at 01:38
  • @djechlin You can provide the missing context. Instead of using “strongly typed” and “weakly typed”, actually describe the restriction you mean. --same post – User Nov 29 '15 at 01:46
  • @User I honestly thought it was pretty understood that "weakly typed" and "strongly typed" are relative adjectives and subjective, not technical descriptions. After you asked about it I added the link which I thought made it clearer. I put the phrase in quotes to emphasize that I was being imprecise. I think this is thorough enough. – djechlin Nov 29 '15 at 01:50