Practical Programming, Third Edition: immutable values are not necessarily unhashable (209)

On page 209 it says: “mutable values are unhashable”
However, mutability and hash-ability are separate properties. A type is hash-able when its hash function is defined. A counter example that shows a mutable list that gets hashed and added as key to a dict:

class mylist(list): # mylist (mutable) is a list with a __hash__ function

    def __hash__(self):
        hash = 0
        for i in self:
            hash ^= i
        return hash

ml= mylist()
ml.append(1)
ml.append(2)
ml.append(3)
print( ml ) # [1, 2, 3]

mydict={ ml:100, mylist([4,5,6]):200}
print( mydict ) # {[1, 2, 3]: 100, [4, 5, 6]: 200}

print( mydict[ml] ) # 100
print( mydict[mylist([4,5,6])] ) # 200
print( mylist([6,7,8]) in mydict) # False

Also a typo:
TypeError: unhashable type: ‘set’ => TypeError: unhashable type: ‘list’

@jmontojo

The “TypeError: unhashable type: ‘list’” error message in Python occurs when you try to use a mutable object (such as a list) as a key in a dictionary. Since dictionaries use keys to index values, keys must be hashable objects, meaning they must be immutable (i.e. their value cannot be changed). Lists are mutable, so they cannot be used as dictionary keys. To fix the error, use an immutable object like a tuple, string, or number as the dictionary key instead.

To resolve the TypeError: unhashable type: ‘list’ error, you need to use an immutable object as a key in a dictionary instead of a mutable one like a list. For example, you can use tuples, strings, or numbers as keys, which are all hashable objects.

d = {[1, 2, 3]: "list_key"} //Using a list as a key in a dictionary
//This will raise the TypeError: unhashable type: 'list' error
d = {(1, 2, 3): "tuple_key"} //Using a tuple as a key in a dictionary
//This will work fine as tuples are hashable