Ethan Furman <[email protected]> added the comment:
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')
----------
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue45473>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com