[Python] How to update/create dynamically enums in Python from a dict? [closed]

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

  1. Stack

    Stack Membro Participativo

    I have a custom enum, but sometimes I want to update it.

    See the example below:

    from enum import StrEnum

    class Colors(StrEnum):
    WHITE = '#ffffff'
    BLACK = '#000000'


    If I have a dict like this: {"red": "#BE3E23", "green": "#47735b"}.

    How can I update this enum definition above?

    My idea is to create a @classmethod function called from_dict that updates the list.

    I'm not sure if I keep the StrEnum inheritance or if I move to a NamedTuple or any other better object to deal with this.

    Continue reading...

Compartilhe esta Página