From 354ecf1ef2283434600a12cf544da6df679d03d9 Mon Sep 17 00:00:00 2001 From: Ethan Furman Date: Wed, 11 Mar 2015 08:43:12 -0700 Subject: [PATCH] Close issue23486: performance boost for enum member lookup --- Lib/enum.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Lib/enum.py b/Lib/enum.py index d744f8f4d5d..5be13d5c71a 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -112,6 +112,10 @@ class EnumMeta(type): enum_class._member_map_ = OrderedDict() # name->value map enum_class._member_type_ = member_type + # save attributes from super classes so we know if we can take + # the shortcut of storing members in the class dict + base_attributes = {a for b in bases for a in b.__dict__} + # Reverse value->name map for hashable values. enum_class._value2member_map_ = {} @@ -165,6 +169,11 @@ class EnumMeta(type): else: # Aliases don't appear in member names (only in __members__). enum_class._member_names_.append(member_name) + # performance boost for any member that would not shadow + # a DynamicClassAttribute + if member_name not in base_attributes: + setattr(enum_class, member_name, enum_member) + # now add to _member_map_ enum_class._member_map_[member_name] = enum_member try: # This may fail if value is not hashable. We can't add the value @@ -468,7 +477,7 @@ class Enum(metaclass=EnumMeta): m for cls in self.__class__.mro() for m in cls.__dict__ - if m[0] != '_' + if m[0] != '_' and m not in self._member_map_ ] return (['__class__', '__doc__', '__module__'] + added_behavior)