Late-binding of function defaults (was Re: What is a function parameter =[] for?)

Chris Angelico rosuav at gmail.com
Thu Nov 26 06:35:54 EST 2015


On Thu, Nov 26, 2015 at 10:23 PM, Marko Rauhamaa <marko at pacujo.net> wrote:
> Chris Angelico <rosuav at gmail.com>:
>
>> On Thu, Nov 26, 2015 at 9:54 PM, Marko Rauhamaa <marko at pacujo.net> wrote:
>>>
>>>    >>> hash([])
>>>    Traceback (most recent call last):
>>>      File "<stdin>", line 1, in <module>
>>>    TypeError: unhashable type: 'list'
>>>
>>> Annoying.
>>
>> Yes, it's really annoying that you get an immediate exception instead
>> of unpredictably getting bizarre failures that depend on the exact
>> bucket sizes and dict size and so on. It's such a pain to get told
>> exactly where the problem is.
>
> The problem is that lists don't have __hash__ and __eq__ defined.

They do define equality. And it depends on their contents, ergo there
cannot be a stable hash. This is NOT a problem. This is the concept of
unhashability behaving EXACTLY CORRECTLY. Sorry to shout, but this is
another case of refusing to accept what has been explained as having
good reason, and I'm getting a little tired of it.

>> Python tends to assume that programmers are intelligent people who are
>> prepared to fix their mistakes.
>
> It's not letting me in the case of list.

Actually it is. Your mistake is trying to use a list as a dict key.
You have a solution available: use a tuple.

ChrisA



More information about the Python-list mailing list