67

I have an enum defined like this:

def enum(**enums):
    return type('Enum', (), enums)

Status = enum(
       STATUS_OK=0,
       STATUS_ERR_NULL_POINTER=1, 
       STATUS_ERR_INVALID_PARAMETER=2)

I have a function that returns status as Status enum. How can I get the name of the enum value, and not just value?

>>> cur_status = get_Status()
>>> print(cur_status)
1

I would like to get STATUS_ERR_NULL_POINTER, instead of 1

martineau
  • 119,623
  • 25
  • 170
  • 301
Mike
  • 931
  • 1
  • 8
  • 15
  • **Note**: if Python standard library's `enum.Enum` is used, refer to this question → [How to convert int to Enum in python? - Stack Overflow](https://stackoverflow.com/questions/23951641/how-to-convert-int-to-enum-in-python) – user202729 Nov 19 '22 at 12:40

5 Answers5

90

You'd have to loop through the class attributes to find the matching name:

name = next(name for name, value in vars(Status).items() if value == 1)

The generator expression loops over the attributes and their values (taken from the dictionary produced by the vars() function) then returns the first one that matches the value 1.

Enumerations are better modelled by the enum library, available in Python 3.4 or as a backport for earlier versions:

from enum import Enum

class Status(Enum):
    STATUS_OK = 0
    STATUS_ERR_NULL_POINTER = 1 
    STATUS_ERR_INVALID_PARAMETER = 2

giving you access to the name and value:

name = Status(1).name  # gives 'STATUS_ERR_NULL_POINTER'
value = Status.STATUS_ERR_NULL_POINTER.value  # gives 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
56

2021 update: These answers are out of date. Using Python's standard Enum class,

cur_status.name

will return the name. (STATUS_ERR_NULL_POINTER)

To look up the enum knowing the name:

s = Status['STATUS_ERR_NULL_POINTER']
gerardw
  • 5,822
  • 46
  • 39
3

You don't need to loop through the Enum class but just access _member_map_.

>>> Status._member_map_['STATUS_OK']
<Status.STATUS_OK: 0>
techneer
  • 147
  • 1
  • 6
3

Not sure which python version it was introduced, but the hidden attribute _value2member_map_ gives you what you want.

class Status(Enum):
    STATUS_OK=0
    STATUS_ERR_NULL_POINTER=1
    STATUS_ERR_INVALID_PARAMETER=2

str(Status._value2member_map_[1])

Out:

'Status.STATUS_ERR_NULL_POINTER'
DeusXMachina
  • 1,239
  • 1
  • 18
  • 26
0

For some reason, most of the methods above did not work for me. All methods return the Enum type as an integer. I'm working with Python 3.7.

In my solution, I defined class function to handle this. It's not purely pythonic, but worked well enough for my case.

from enum import Enum
    
class Status(Enum):
    STATUS_OK = 0
    STATUS_ERR_NULL_POINTER = 1 
    STATUS_ERR_INVALID_PARAMETER = 2
    
    @classmethod
    def name(cls,val):
        return { v:k for k,v in dict(vars(cls)).items() if isinstance(v,int)}.get(val,None)

# test it
stat = Status.STATUS_OK
print(Status.name(stat))

Prints: 'STATUS_OK'

It may seem obvious that we asked for the status after giving it the status, but in my case, this is set programmatically elsewhere

RexBarker
  • 1,456
  • 16
  • 14