Enum source code

From the above example, you can know that all members of the enumeration class can be traversed through __members__. So why?

We can first take a rough look at how the source code of Enum is implemented; Enum is in the module enum.py, let’s first take a look at the fragment of the Enum class

class Enum(metaclass=EnumMeta):
    """Generic enumeration.
    Derive from this class to define new enumerations.
    """

As you can see, Enum is Inherit the metaclass EnumMeta; look at the relevant fragments of EnumMeta again

class EnumMeta(type):
    """Metaclass for Enum"""
    @property
    def __members__(cls):
        """Returns a mapping of member name->value.
        This mapping lists all enum members, including aliases. Note that this
        is a read-only view of the internal mapping.
        """
        return MappingProxyType(cls._member_map_)

First of all, the __members__ method returns a MappingProxyType that contains a Dict or Map, and the method __members__(cls) is accessed through @property Changed to a variable form, which can be accessed directly through __members__

Continuing Learning
||
submitReset Code