[Python] Python Enum class - Use Enum name argument to select Enum value within a function

Discussão em 'Python' iniciado por Stack, Fevereiro 18, 2025.

  1. Stack

    Stack Membro Participativo

    I'm using Python 3.9. I have an Enum class like this:

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


    I want to use this enum class in a function with an argument which is a valid Enum name:

    def some_func(color_name):
    return Color.color_name.value


    So, when I call the function:

    >>> some_func(RED)


    why does it not work?

    Do I need to create a method for the Color class? That seems to defeat the purpose of have it as an Enum class.

    Continue reading...

Compartilhe esta Página