kopia lustrzana https://github.com/micropython/micropython-lib
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
rodzic
56dd0f94e9
commit
69b65efd7e
|
@ -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)
|
||||
|
|
|
@ -6,3 +6,5 @@ d[2] = 3
|
|||
assert d[2] == 3
|
||||
del d[1]
|
||||
assert d[1] == 42
|
||||
|
||||
assert "foo" not in d
|
||||
|
|
Ładowanie…
Reference in New Issue