Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 34ae04f

Browse files
asvetlovethanfurman
authored andcommitted
Speed-up building enums by value, e.g. http.HTTPStatus(200) (#11318)
bpo-35585: Speed up enum by-value lookup
1 parent 3a81076 commit 34ae04f

2 files changed

Lines changed: 5 additions & 2 deletions

File tree

Lib/enum.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,8 +563,10 @@ def __new__(cls, value):
563563
# by-value search for a matching enum member
564564
# see if it's in the reverse mapping (for hashable values)
565565
try:
566-
if value in cls._value2member_map_:
567-
return cls._value2member_map_[value]
566+
return cls._value2member_map_[value]
567+
except KeyError:
568+
# Not found, no need to do long O(n) search
569+
pass
568570
except TypeError:
569571
# not there, now do long search -- O(n) behavior
570572
for member in cls._member_map_.values():
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Speed-up building enums by value, e.g. http.HTTPStatus(200).

0 commit comments

Comments
 (0)