gh-118932: ChainMap.__contains__ performance improvement (gh-118946)

This commit is contained in:
d.grigonis 2024-05-11 23:55:23 +03:00 committed by GitHub
parent 7e894c2f38
commit cd4cfa6ed2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 5 additions and 2 deletions

View File

@ -1016,7 +1016,7 @@ class ChainMap(_collections_abc.MutableMapping):
return self.__missing__(key) # support subclasses that define __missing__
def get(self, key, default=None):
return self[key] if key in self else default
return self[key] if key in self else default # needs to make use of __contains__
def __len__(self):
return len(set().union(*self.maps)) # reuses stored hash values if possible
@ -1028,7 +1028,10 @@ class ChainMap(_collections_abc.MutableMapping):
return iter(d)
def __contains__(self, key):
return any(key in m for m in self.maps)
for mapping in self.maps:
if key in mapping:
return True
return False
def __bool__(self):
return any(self.maps)