[Python] Enum class with function values ends up with empty members list [duplicate]

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

  1. Stack

    Stack Membro Participativo

    It is my understanding that Python Enum values can be any Python object. However, when I run the following

    from enum import Enum

    def f():
    pass

    def g():
    pass

    def h():
    pass

    class LossType(Enum):
    MSE = f
    MAE = g
    CROSS_ENTROPY = h

    print(list(LossType.__members__.keys()))


    the output is [].

    The same does not happen when we use integer values:

    class LossType(Enum):
    MSE = 1
    MAE = 2
    CROSS_ENTROPY = 3
    print(list(LossType.__members__.keys()))


    produces ['MSE', 'MAE', 'CROSS_ENTROPY']. Why?

    Continue reading...

Compartilhe esta Página