Skip to content Skip to sidebar Skip to footer

Why Do I Run Into An Infinite Loop Without __contains__?

The following code runs into an infinite loop: class SubCommandMap: def __init__(self): self._command = dict() def __getitem__(self, key): return self._co

Solution 1:

In the absence of __contains__ the in operator will use the __getitem__ method you have defined to see if it gets the object you're looking for. It will pass every int in turn to __getitem__ until it finds the item or the method raises an exception. If neither of those happens, you have an infinite loop.


Post a Comment for "Why Do I Run Into An Infinite Loop Without __contains__?"