[issue45473] Enum add "from_name" and "from_value" class methods
New submission from Aaron Koch : Documentation: https://docs.python.org/3/library/enum.html#creating-an-enum Current behavior: SomeEnum[name] is used to construct an enum by name SomeEnum(value) is used to construct an enum by value Problem: As a user of enums, it is difficult to remember the mapping between parenthesis/square brackets and construct from name/construct from value. Suggestion: Add two class methods to Enum @classmethod def from_name(cls, name): return cls[name] @classmethod def from_value(cls, value): return cls(value) Benefits: This is an additive change only, it doesn't change any behavior of the Enum class, so there are no backwards compatibility issues. Adding these aliases to the Enum class would allow readers and writers of enums to interact with them more fluently and with less trips to the documentation. Using these aliases would make it easier to write the code you intended and to spot bugs that might arise from the incorrect use of from_name or from_value. -- messages: 403936 nosy: aekoch priority: normal severity: normal status: open title: Enum add "from_name" and "from_value" class methods type: enhancement ___ Python tracker <https://bugs.python.org/issue45473> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue45473] Enum add "from_name" and "from_value" class methods
Aaron Koch added the comment: Are there any other names that you would contemplate besides `from_name` and `from_value`? My reading of your response indicates that you are fundamentally opposed to the addition of class methods, since they would limit the space of possible instance methods/members. Is that a fair reading? If it is not, would you be open to different method names? Do you agree with the fundamental issue that is identified: that the parenthesis/square bracket construction is difficult to read and makes implementation mistakes more likely? That it would be good to have some what to make it more explicit at both read and write time whether the enum is being constructed using the name or the value. One alternative to the class methods I might propose is to use a keyword argument in the __init__ function. SomeEnum(name="foo") SomeEnum(value="bar") This would also solve the stated problem, but I suspect that messing with the init function introduces more limitations to the class than the classmethod solution. -- ___ Python tracker <https://bugs.python.org/issue45473> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com