[Python] Python 3 Enums with Function Values

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

  1. Stack

    Stack Membro Participativo

    I noticed an oddity in the Python 3 Enums (link).
    If you set the value of an Enum to a function, it prevents the attribute from being wrapped as an Enum object, which prevents you from being able to use the cool features like EnumCls['AttrName'] to dynamically load the attribute.

    Is this a bug? Done on purpose?
    I searched for a while but found no mention of restricted values that you can use in an Enum.

    Here is sample code that displays the issue:

    class Color(Enum):
    Red = lambda: print('In Red')
    Blue = lambda: print('In Blue')

    print(Color.Red) # <function> - should be Color.Red via Docs
    print(Color.Blue) # <function> - should be Color.Bluevia Docs
    print(Color['Red']) # throws KeyError - should be Color.Red via Docs

    Continue reading...

Compartilhe esta Página