collections.defaultdict: Implement __contains__.

Otherwise, "in" operation uses list protocol (enumeration using
__getitem__, which adds a key/value pair to teh underlying dict)
and ends with memory overflow.
pull/149/head
Paul Sokolovsky 2017-01-26 01:26:40 +03:00
rodzic 56dd0f94e9
commit 69b65efd7e
2 zmienionych plików z 5 dodań i 0 usunięć

Wyświetl plik

@ -27,6 +27,9 @@ class defaultdict:
def __delitem__(self, key):
del self.d[key]
def __contains__(self, key):
return key in self.d
def __missing__(self, key):
if self.default_factory is None:
raise KeyError(key)

Wyświetl plik

@ -6,3 +6,5 @@ d[2] = 3
assert d[2] == 3
del d[1]
assert d[1] == 42
assert "foo" not in d