Close issue23486: performance boost for enum member lookup

This commit is contained in:
Ethan Furman 2015-03-11 08:43:12 -07:00
parent ca8b64461d
commit 354ecf1ef2
1 changed files with 10 additions and 1 deletions

View File

@ -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)