New submission from Aaron Koch <aaronkoc...@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 <rep...@bugs.python.org>
<https://bugs.python.org/issue45473>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to