2

I am new to Python and just stumbled on adding list to set. Now I know the reason why this is not possible is that list is mutable so its hashcode (or the Python corresponding concept) will change. Reflecting on my programming in Java, I found that I have done this many times.

HashSet<ArrayList<String>> masterCollection = new HashSet<ArrayList<String>>();
ArrayList<String> a = new ArrayList<String>();
masterCollection.add(a);
a.add("Hello, World");
for(ArrayList<String> list : masterCollection) {
    // do something to list
}

I did not notice any abnormal behaviors of the above code. I even used ArrayList as keys to HashMap and succeeded in accomplishing my task.

Therefore, I want to ask how internally Python is different in Java in this aspect and whether there are something I need to pay attention to when writing the above code.

4
  • 3
    Languages are different. All languages don't behave the same way even if they use the same terminology.
    – Kayaman
    Commented Jul 19, 2014 at 23:29
  • @Kayaman I know they are different and I am aware of this difference. I want to know how Java accomplishes this while Python does not. Now the way Python works seems more reasonable: the set should not be responsible of monitoring the hashcode of its elements all the time and change accordingly. But I don't think there is a mechanism for ArrayList to report its hashcode change every time its element changes. So how does it work?
    – zyl1024
    Commented Jul 19, 2014 at 23:34
  • 2
    It's a known fact, that if an object changes its hashcode while it's in a HashSet (or similar hash-related construct) the results are undefined. That's why you should only put immutable things in them.
    – Kayaman
    Commented Jul 19, 2014 at 23:37
  • @zyl1024 actually what you're trying to do doesn't make sense unless you have a very specific (and bizarre) use-case: the usage of set enforces the uniqueness of the lists (any two lists in the set should NOT be identical). But, if the lists keep changing - how do you enforce that ? now take the same problem to a multithreaded environment and you got yourself into a real mess...
    – Nir Alfasi
    Commented Jul 19, 2014 at 23:42

2 Answers 2

1

Lists are unhashable in python because they do not define __hash__. There are reasons for that, but you could conceivably define your own mutable class that does, provided that said hash does not change across the lifetime of the object. For example, using id(x) as the hash. In fact, you've probably unknowingly implemented exactly that in a lot of your own classes.

I'll quote from the docs on "hashable":

All of Python’s immutable built-in objects are hashable, while no mutable containers (such as lists or dictionaries) are. Objects which are instances of user-defined classes are hashable by default; they all compare unequal (except with themselves), and their hash value is their id().

Mutability and hashability are coupled, but not at all the same thing.

0

This post explained it clearly. In a nutshell, Java and Python all use an internal hash_table to implement a set, and use hash code as key, the difference is Python require the variable to be hashed being immutable, and Java not, which can cause wired behaviour:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.