Get Enum name from multiple values python

The easiest way is to use the aenum library1, which would look like this:

from aenum import MultiValueEnum

class DType(MultiValueEnum):
    float32 = "f", 8
    double64 = "d", 9

and in use:

>>> DType("f")
<DType.float32: 'f'>

>>> DType(9)
<DType.double64: 'd'>

As you can see, the first value listed is the canonical value, and shows up in the repr().

If you want all the possible values to show up, or need to use the stdlib Enum (Python 3.4+), then the answer found here is the basis of what you want (and will also work with aenum):

class DType(Enum):
    float32 = "f", 8
    double64 = "d", 9

    def __new__(cls, *values):
        obj = object.__new__(cls)
        # first value is canonical value
        obj._value_ = values[0]
        for other_value in values[1:]:
            cls._value2member_map_[other_value] = obj
        obj._all_values = values
        return obj

    def __repr__(self):
        return '<%s.%s: %s>' % (
                self.__class__.__name__,
                self._name_,
                ', '.join([repr(v) for v in self._all_values]),
                )

and in use:

>>> DType("f")
<DType.float32: 'f', 8>

>>> Dtype(9)
<DType.float32: 'd', 9>

1 Disclosure: I am the author of the Python stdlib Enum, the enum34 backport, and the Advanced Enumeration (aenum) library.

Leave a Comment