Skip to content

Commit

Permalink
gh-118932: ChainMap.__contains__ performance improvement (gh-118946)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgrigonis committed May 11, 2024
1 parent 7e894c2 commit cd4cfa6
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions Lib/collections/__init__.py
Expand Up @@ -1016,7 +1016,7 @@ def __getitem__(self, key):
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
Expand All @@ -1028,7 +1028,10 @@ def __iter__(self):
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)
Expand Down

0 comments on commit cd4cfa6

Please sign in to comment.