Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python enum key string get

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


or

#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)
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #python #enum #key #string
ADD COMMENT
Topic
Name
5+4 =