I’ve come across this strange error that I can’t explain.
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import UserDict
>>> a = UserDict.UserDict()
>>> b = {}
>>> b[a]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable
I understand that this should be an error. I don’t understand why it says 'NoneType' object is not callable. As far as I can tell I’m not calling anything in the line that causes the error.
I expected the error would be something more like this:
>>> b[b]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'dict'
Can someone please explain this to me before I go insane?
Looking at
UserDictimplementation as suggested by @Wooble, I see this:Therefore it’s true that the problem is because of the implementation of
UserDict.If you really need to use your own dictionary type, I suggest to subclass directly from
dictand implement your own__hash__method or, alternatively, transform the dictionary into a hashable object with the help of, for example,frozenset: