diff options
author | Ethan Furman <ethan@stoneleaf.us> | 2015-03-11 15:43:12 (GMT) |
---|---|---|
committer | Ethan Furman <ethan@stoneleaf.us> | 2015-03-11 15:43:12 (GMT) |
commit | 354ecf1ef2283434600a12cf544da6df679d03d9 (patch) | |
tree | aebe8ad8066f63482698248692c76df5d186e402 /Lib/enum.py | |
parent | ca8b64461db5469fc4dc7e43cacb747046a0be53 (diff) | |
download | cpython-354ecf1ef2283434600a12cf544da6df679d03d9.zip cpython-354ecf1ef2283434600a12cf544da6df679d03d9.tar.gz cpython-354ecf1ef2283434600a12cf544da6df679d03d9.tar.bz2 |
Close issue23486: performance boost for enum member lookup
Diffstat (limited to 'Lib/enum.py')
-rw-r--r-- | Lib/enum.py | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/Lib/enum.py b/Lib/enum.py index d744f8f..5be13d5 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) |