close

How to get back name of the enum element?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to get back name of the enum element in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to get back name of the enum element?

  1. How to get back name of the enum element?

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

  2. get back name of the enum element

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

Method 1

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']

Method 2

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'

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read