9

I have an enum like this

class testEnum(Enum):
   Code_1 = "successful response"
   Code_2 = "failure response"

Then I have a method that takes the enum key name Code_1 and enum key value successful response as inputs.

If I send testEnum.Code_1 then that resolves to successful response and not Code_1.

I checked some documentation online that suggests to use testEnum.Code_1.name but that throws an error saing that 'name' doesn't exist for the enum item.

Does anyone know how to get the name of the enum key ?

4
  • 2
    I can't reproduce the error. Commented Jun 8, 2017 at 23:17
  • 1
    Are you using the standard distribution of python? Which version? Commented Jun 8, 2017 at 23:20
  • I cannot reproduce the error in Python 3.5.2 nor on Python 2.7.12. Commented Jun 8, 2017 at 23:20
  • 3
    If your getting a traceback, post the full error verbatim. Commented Jun 8, 2017 at 23:22

4 Answers 4

10

I suspect that what's happened is that you're using the outdated pip-installable library called enum. If you did, you'd get something like

>>> from enum import Enum
>>> class testEnum(Enum):
...    Code_1 = "successful response"
...    Code_2 = "failure response"
... 
>>> testEnum.Code_1
'successful response'
>>> testEnum.Code_1.name
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'name'

whereas with the "real" enum (either enum in the standard library if you're using modern Python, or the enum34 backport if you're not), you'd see

>>> from enum import Enum
>>> class testEnum(Enum):
...    Code_1 = "successful response"
...    Code_2 = "failure response"
... 
>>> testEnum.Code_1
<testEnum.Code_1: 'successful response'>
>>> testEnum.Code_1.name
'Code_1'

You can confirm this independently by typing help(enum) and seeing whether you see "NAME / enum / MODULE REFERENCE / https://docs.python.org/3.6/library/enum" (as you should) or simply "NAME / enum - Robust enumerated type support in Python" if you're using the older one.

Sign up to request clarification or add additional context in comments.

5 Comments

you are right, I did pip install enum. Looks like that is the old one ? So I should be doing pip install enum34 instead ?
@Hary: really, you should be using a modern Python! ;-) But yes, enum34 should work for the moment.
I am using python version 2.7. would that not suffice ? Do I still need to do pip install enum34 in python 2.7 ?
I was indeed using an old version of enum. I installed enum34 and it worked as you explained. Wondering if these enums aren't part of standard python distribution and not sure why such standard constructs like enumerations need to be separately installed ?
We began to move to python3 10 years ago :) enum went into python in 3.4.
5

You can start your investigation with the __dict__ that comes with your object. Interesting reading is found with

print(testEnum.__dict__)

In that dict you will see a good start which you can test with the following:

print(testEnum._member_names_)

which, indeed, yields

['Code_1', 'Code_2']

Comments

4

in python3.9 can use:

testEnum.__members__

Comments

1

dir(testEnum) will give you the dictionary keys.

e.g.

dir(testEnum)

returns:

['Code_1', 'Code_2', 'class', 'delattr', 'dict', 'dir', 'doc', 'eq', 'format', 'ge', 'getattribute', 'gt', 'hash', 'init', 'init_subclass', 'le', 'lt', 'module', 'ne', 'new', 'reduce', 'reduce_ex', 'repr', 'setattr', 'sizeof', 'str', 'subclasshook', 'weakref']

6 Comments

I'm not exactly sure how this answers the question. Could you elaborate?
OP seems to be looking for a way to return "Code_1" and "Code_2". dir(testEnum)[0] will return Code_1, for example.
OK, I see what you were trying to do. But he was trying to get the names from doing testEnum.Code_1, like the documentation said he could.
No, he didn't. His code should work because he in inheriting from the enum.Enum class from the standard library.
If I have an enum then, converting it to a dictionary and referencing to it's members using dictionary index like [0] is counter intuitive and defeats the whole purpose of using an enum. Using an enum lets the code talk to the user when you do enum.code_1 rather than doing dict(enum)[0]
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.