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

Skip to content

Commit 705b599

Browse files
Speed-up building enums by value, e.g. http.HTTPStatus(200) (GH-11318) (GH-11324)
bpo-35585: Speed up enum by-value lookup (cherry picked from commit 34ae04f) Co-authored-by: Andrew Svetlov <[email protected]>
1 parent dcf14d1 commit 705b599

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
@@ -532,8 +532,10 @@ def __new__(cls, value):
532532
# by-value search for a matching enum member
533533
# see if it's in the reverse mapping (for hashable values)
534534
try:
535-
if value in cls._value2member_map_:
536-
return cls._value2member_map_[value]
535+
return cls._value2member_map_[value]
536+
except KeyError:
537+
# Not found, no need to do long O(n) search
538+
pass
537539
except TypeError:
538540
# not there, now do long search -- O(n) behavior
539541
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)