Home > Software engineering >  `dict().values in dict()` fails in Python 3.7 but succeeds in Python 3.11
`dict().values in dict()` fails in Python 3.7 but succeeds in Python 3.11

Time:06-05

on Python 3.7.13

dict().values in dict()

gives,

TypeError: unhashable type: 'dict'

on Python 3.11,

dict().values in dict()

gives,

False

what is the reason for this change?

CodePudding user response:

The result of the expression dict().values is a "method wrapper"; an object that contains a method and the object on which the method is to be called.

If you do, for example,

hash(dict().__str__)

you get an exception in 3.7, but not in later versions. The difference is in the function wrapper_hash in CPython which has changed from hashing the wrapped object — hence the 'dict' in the error message even though we are not (explicitly) hashing a dict — to using its identity instead.

  • Related