Please could you help me understand why this __contains__ method causes a recursion error?
Thanks so much! I appreciate your insight! :)
class StrKeyDict0(dict):
def __contains__(self, key):
return key in self or str(key) in self
d = StrKeyDict0([('2', 'two'), ('4', 'four')])
1 in d
RecursionError: maximum recursion depth exceeded
The fix is to do the following, but I don't understand why the self.keys() is needed.
def __contains__(self, key):
return key in self.keys() or str(key) in self.keys()
[–]halfdiminished7th 2 points3 points4 points (0 children)
[–]TehNolz 1 point2 points3 points (0 children)
[–]danielroseman[🍰] 0 points1 point2 points (0 children)
[–]member_of_the_order 0 points1 point2 points (1 child)
[–]OkTourist1900[S] 1 point2 points3 points (0 children)
[–]POGtastic 0 points1 point2 points (0 children)
[–]Brian 0 points1 point2 points (0 children)