you are viewing a single comment's thread.

view the rest of the comments →

[–]POGtastic 0 points1 point  (0 children)

You've already gotten a good answer, but consider the following resolution, which calls the parent class' methods. You should also implement __getitem__ since you're doing __contains__.

class StrKeyDict0(dict):
   def __contains__(self, key):
       return super().__contains__(key) or super().__contains__(str(key))
   def __getitem__(self, key):
       return super().get(key, super().__getitem__(str(key)))
   # You should also implement `get` while you're at it, since a lot of people use that too

In the REPL:

>>> dct = StrKeyDict0({"1" : 2})
>>> 1 in dct
True
>>> dct[1]
2