[New-bugs-announce] [issue45473] Enum add "from_name" and "from_value" class methods

Aaron Koch report at bugs.python.org
Thu Oct 14 15:11:28 EDT 2021


New submission from Aaron Koch <aaronkoch99 at yahoo.com>:

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 <report at bugs.python.org>
<https://bugs.python.org/issue45473>
_______________________________________


More information about the New-bugs-announce mailing list