20

I'm trying to get the name of a enum given one of its multiple values:

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

when I try to get one value giving the name it works:

print DType["float32"].value[1]  # prints 8
print DType["float32"].value[0]  # prints f

but when I try to get the name out of a given value only errors will come:

print DataType(8).name
print DataType("f").name

raise ValueError("%s is not a valid %s" % (value, cls.name))

ValueError: 8 is not a valid DataType

ValueError: f is not a valid DataType

Is there a way to make this? Or am I using the wrong data structure?

1
  • Enums can't have multiple value per name. Why do you need this, is one of these an alias? When would use use 'f' and when 9?
    – Martijn Pieters
    Apr 4, 2017 at 9:10

1 Answer 1

40

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.

6
  • 1
    @Ruvalcaba: Glad to help. When an answer works for you, you should accept it (click on the green check mark by that answer's score). Apr 25, 2017 at 14:16
  • Just change the import to: from aenum import MultiValueEnum class DType(MultiValueEnum): float32 = "f", 8 double64 = "d", 9
    – epinal
    Jun 29, 2017 at 17:33
  • @EthanFurman Do you think MultiValueEnum might land into the standard library eventually?
    – Rotareti
    Apr 15, 2018 at 13:45
  • 2
    @Rotareti: Hard to say. Having real-world use-cases would help prove the need, but even then Enum is meant as a building-block more than a whole bunch of comprehensive solutions. Apr 15, 2018 at 22:47
  • 2
    How to get "8" or "9"?
    – Nairum
    Aug 25, 2020 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.