【发布时间】:2020-04-18 10:02:53
【问题描述】:
下面的代码会陷入死循环:
class SubCommandMap:
def __init__(self):
self._command = dict()
def __getitem__(self, key):
return self._command.get(key)
def __setitem__(self, key, value):
self._command[key] = value
m = SubCommandMap()
" " in m # <- why is this an infinite loop?
当然这是一个错误。 m 被认为是不同类型的不同对象。
但是为什么这会导致无限循环而不是抛出异常呢?
我添加了以下方法:
def __contains__(self, other):
raise NotImplementedError()
现在我收到了相应的错误消息。
还有其他类似的情况我需要小心避免无限循环吗?
【问题讨论】:
标签: python infinite-loop