Message403949
The problem with adding methods is that those names are then unavailable for member names -- at least not without possible confusion.
Also, unlike the `name` and `value` instance attributes which do not hide class attributes (aka members) of the same name, `from_name` is a class method so we couldn't have both a `from_name()` class method and a `from_name` class member at the same time.
If those are issues you know wouldn't a problem for you, you can create your own base Enum class with those methods defined, and then inherit from them:
class EasyEnum(Enum):
@classmethod
def from_name(cls, name):
return cls[name]
# etc
class MyEnum(EasyEnum):
RED = 1
# etc.
On the other hand, if you want to use this with any enum, then you'll need to make your own helper functions:
def from_name(enum, name):
return enum[name]
enum_member = from_name(MyEnum, 'RED') |
|
Date |
User |
Action |
Args |
2021-10-14 21:16:06 | ethan.furman | set | recipients:
+ ethan.furman, aekoch |
2021-10-14 21:16:06 | ethan.furman | set | messageid: <1634246166.51.0.359307459214.issue45473@roundup.psfhosted.org> |
2021-10-14 21:16:06 | ethan.furman | link | issue45473 messages |
2021-10-14 21:16:06 | ethan.furman | create | |
|