Speed-up building enums by value, e.g. http.HTTPStatus(200) (#11318) · python/cpython@34ae04f (original) (raw)

Original file line number Diff line number Diff line change
@@ -563,8 +563,10 @@ def __new__(cls, value):
563 563 # by-value search for a matching enum member
564 564 # see if it's in the reverse mapping (for hashable values)
565 565 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
568 570 except TypeError:
569 571 # not there, now do long search -- O(n) behavior
570 572 for member in cls._member_map_.values():