In python, a tuple containing a mutable object (like a list) is not hashable.
In [1]: t = (1,2,[3])
In [2]: hash(t)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-2-8a01c6d8dba1> in <module>
----> 1 hash(t)
TypeError: unhashable type: 'list'
But why it is not correct for functions with mutable params?
In [5]: def f(a: List = [1]):
...: a += a
...: return a
...:
In [6]: hash(f)
Out[6]: 8731603958411
Every time this function is called, the a
parameter changes, but the function f
is still hashable and considered to be immutable.
What is the difference between functions and tuples in this case?