[Python] How to update name attribute in python enum?

Discussão em 'Python' iniciado por Stack, Setembro 16, 2024.

  1. Stack

    Stack Membro Participativo

    When doing a basic Enum in python:

    from enum import Enum
    class Color(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3

    Color.RED.name # "RED"


    the name attribute lets get a string representation of the item which is exactly the strigified name of the attribute: RED => "RED"

    Is there a way to update this rule with, for instance, a dict or a factory, so as to have for example:

    Color.RED.name # "my color is RED"

    Continue reading...

Compartilhe esta Página