> 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.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/Z56NRWE2R4NBUGGXJZTMZ5W5TGCDFPT6/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to