
Per the AutoName example at https://docs.python.org/3/library/enum.html#using-automatic-values, one can easily use _generate_next_value_ to derive a member's value from its name, but that is only invoked when the object's value is an instance of auto(), so if you want to give an enum member an explicit value _and_ have an attribute such as label derived from its name, that's a hard nut to crack. An example usage of what I'm suggesting would be something like⦠class ChoiceEnum(Enum): _use_context_ = True
def __new__(cls, value, context): obj = object.__new__(cls) obj._value_ = value obj.label = context.name.capitalize()
class Color(ChoiceEnum): SMALL = 'S' MEDIUM = 'M' LARGE = 'L'
print(Color.SMALL.label) # Prints 'Small'
My though about `_use_context_ = True` is not _only_ about backward compatibility. It is also about not making simpler cases deal with the fact that the `context` argument will be passed to `__new__` though I guess one could argue that cases where `__new__` is used are already not simple.