[Python-Dev] dict contains raises TypeError on unhashable input (original) (raw)
Nick Coghlan ncoghlan at gmail.com
Sat Jul 20 09:39:29 CEST 2013
- Previous message: [Python-Dev] dict __contains__ raises TypeError on unhashable input
- Next message: [Python-Dev] dict __contains__ raises TypeError on unhashable input
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 20 July 2013 09:47, Ethan Furman <ethan at stoneleaf.us> wrote:
While working on issue #18508 I stumbled across this:
Traceback (most recent call last): ... File "/usr/local/lib/python3.4/enum.py", line 417, in new if value in cls.value2membermap: TypeError: unhashable type: 'list' I'll wrap it in a try-except block, but I must admit I was surprised the answer wasn't False. After all, if the input is unhashable then obviously it's not in the dict; furthermore, if I were to compare the number 5 with a set() I would get False, not a TypeMismatch error, and dict keys are basically done by equality, the hash is just (?) a speed-up.
"in" is controlled entirely by the container. Both set() and dict() consider passing them an unhashable object to be a categorical type error:
[] in set() Traceback (most recent call last): File "", line 1, in TypeError: unhashable type: 'list' [] in {} Traceback (most recent call last): File "", line 1, in TypeError: unhashable type: 'list'
Equality is different, because people do equality comparisons between unlike types all the time, so we try very hard to ensure "I do not understand this type" is reported as False rather than "I don't know" (as indicated by an exception). It's basically the only place where we do that - pretty much everywhere else, we err on the side of "I do not understand what this even means" translating to an exception (Python 3 took another big step in that direction by making sure ordered comparisons between unlike types threw TypeError by default, instead of returning a dubious answer).
Cheers, Nick.
-- Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
- Previous message: [Python-Dev] dict __contains__ raises TypeError on unhashable input
- Next message: [Python-Dev] dict __contains__ raises TypeError on unhashable input
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]