mirror of
https://github.com/python/cpython.git
synced 2024-11-27 11:55:13 +08:00
gh-118932: ChainMap.__contains__ performance improvement (gh-118946)
This commit is contained in:
parent
7e894c2f38
commit
cd4cfa6ed2
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user