[Python] Convert string to Enum in Python

Discussão em 'Python' iniciado por Stack, Julho 24, 2025.

  1. Stack

    Stack Membro Participativo

    What's the correct way to convert a string to a corresponding instance of an Enum subclass? Seems like getattr(YourEnumType, str) does the job, but I'm not sure if it's safe enough.

    As an example, suppose I have an enum like

    class BuildType(Enum):
    debug = 200
    release = 400


    Given the string 'debug', how can I get BuildType.debug as a result?

    Continue reading...

Compartilhe esta Página