mirror of
https://github.com/python/cpython.git
synced 2024-11-23 09:54:58 +08:00
Add __reversed__ to Enum. Minor code reorg (moved __members__ to be in alpha order).
This commit is contained in:
parent
5589bd109a
commit
2131a4a2fc
23
Lib/enum.py
23
Lib/enum.py
@ -225,16 +225,6 @@ class EnumMeta(type):
|
||||
def __dir__(self):
|
||||
return ['__class__', '__doc__', '__members__'] + self._member_names_
|
||||
|
||||
@property
|
||||
def __members__(cls):
|
||||
"""Returns a mapping of member name->value.
|
||||
|
||||
This mapping lists all enum members, including aliases. Note that this
|
||||
is a read-only view of the internal mapping.
|
||||
|
||||
"""
|
||||
return MappingProxyType(cls._member_map_)
|
||||
|
||||
def __getattr__(cls, name):
|
||||
"""Return the enum member matching `name`
|
||||
|
||||
@ -260,9 +250,22 @@ class EnumMeta(type):
|
||||
def __len__(cls):
|
||||
return len(cls._member_names_)
|
||||
|
||||
@property
|
||||
def __members__(cls):
|
||||
"""Returns a mapping of member name->value.
|
||||
|
||||
This mapping lists all enum members, including aliases. Note that this
|
||||
is a read-only view of the internal mapping.
|
||||
|
||||
"""
|
||||
return MappingProxyType(cls._member_map_)
|
||||
|
||||
def __repr__(cls):
|
||||
return "<enum %r>" % cls.__name__
|
||||
|
||||
def __reversed__(cls):
|
||||
return (cls._member_map_[name] for name in reversed(cls._member_names_))
|
||||
|
||||
def __setattr__(cls, name, value):
|
||||
"""Block attempts to reassign Enum members.
|
||||
|
||||
|
@ -477,6 +477,13 @@ class TestEnum(unittest.TestCase):
|
||||
[Season.SUMMER, Season.WINTER, Season.AUTUMN, Season.SPRING],
|
||||
)
|
||||
|
||||
def test_reversed_iteration_order(self):
|
||||
self.assertEqual(
|
||||
list(reversed(self.Season)),
|
||||
[self.Season.WINTER, self.Season.AUTUMN, self.Season.SUMMER,
|
||||
self.Season.SPRING]
|
||||
)
|
||||
|
||||
def test_programatic_function_string(self):
|
||||
SummerMonth = Enum('SummerMonth', 'june july august')
|
||||
lst = list(SummerMonth)
|
||||
|
Loading…
Reference in New Issue
Block a user