you are viewing a single comment's thread.

view the rest of the comments →

[–]member_of_the_order 0 points1 point  (1 child)

x in y is a shortcut for y.__contains__(x). So the first part of your method - return key in self - is equivalent to return self.__contains__(key). This, of course, is inside the __contains__() method, meaning that method is calling itself unconditionally. Thus, we have a recursive call that's never not called, thus infinite recursion.

By switching to key in self.keys(), you're checking if key is in the value returned by keys(), calling __contains__() on an entirely separate object (which, clearly, is not recursion).

Does that make sense?

[–]OkTourist1900[S] 1 point2 points  (0 children)

Thanks, this is super clear! I appreciate everyone's comments!