12

I have an enum that I define like this:

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

an_enum = make_enum(first=1, second=2)

At some later point I would like to check, if a value that I took as a parameter in a funciton is part of an_enum. Usually i would do it like this

assert 1 in to_list(an_enum)

How can I convert the enum object an_enum to a list? If that is not possible, how can I check if a value "is part of the enum"?

4
  • 2
    docs.python.org/3/library/enum.html#iteration Commented Jan 9, 2019 at 12:49
  • @meowgoesthedog Already thought about that. I think you can only use that, if you are working with the class of the enum. Since I do not define a class, that does not seem to be possible Commented Jan 9, 2019 at 12:52
  • Try type(an_enum) to get the class? Commented Jan 9, 2019 at 12:55
  • 1
    Beginning in Python 3.4 there is an actual Enum type that supports list(an_enum) plus much more. Commented Jan 9, 2019 at 17:50

3 Answers 3

17

Python's Enum object has build-in enumerable.name and enumerable.value attributes for each member of an Enum.

an_enum = Enum('AnEnum', {'first': 1, 'second': 2})


[el.value for el in an_enum]
# returns: [1, 2]

[el.name for el in an_enum]
# returns: ['first', 'second']

Sidenode: Be careful with assert. If someone runs your script with python -O asserts will never fail.

To check if a value is part of an enum:

if 1 in [el.value for el in an_enum]:
    pass
Sign up to request clarification or add additional context in comments.

Comments

5

How can I convert the enum object an_enum to a list?

>>> [name for name in dir(an_enum) if not name.startswith('_')]
['first', 'second']

How can I check if a value "is part of the enum"?

>>> getattr(an_enum, 'first')
1
>>> getattr(an_enum, '1')
Traceback [...] 
AttributeError: type object 'Enum' has no attribute '1'

3 Comments

I'm pretty sure that OP is asking for enum value membership check, not name check
So the above version would give you al the attributes. But this really helped, because you can simply change that to [getattr(an_enum, name) for name in dir(an_enum) if not name.startswith('_')] and you will get what I need [1, 2]
[element.name for element in an_enum] is more consise
4

I'm not sure why you're defining enums like you do, there's supported functional way to do this:

en_enum = Enum('Numbers', {'first': 1, 'second': 2})

If this suits your needs, you can do

>>> en_enum(1)
<Numbers.first: 1>

>>> en_enum(3)
ValueError: 3 is not a valid Numbers

not actually membership check, but you don't need any special methods/transformers

1 Comment

Thank you very much for the suggesion. I think I will stay with [getattr(an_enum, name) for name in dir(an_enum) if not name.startswith('_')], but this way of writing enums is a lot nicer for sure. Also I did not know that you can simply get the enums elements by doing an_enum(x). So thanks!

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.