Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all values from python enum class?

Tags:

python

enums

I'm using Enum4 library to create an enum class as follows:

class Color(Enum):     RED = 1     BLUE = 2 

I want to print [1, 2] as a list somewhere. How can I achieve this?

like image 495
user1159517 Avatar asked Apr 07 '15 23:04

user1159517


People also ask

How do I get a list of all enum values?

Get a list of Enum members. The idea is to use the Enum. GetValues() method to get an array of the enum constants' values. To get an IEnumerable<T> of all the values in the enum, call Cast<T>() on the array.

How do you access an enum in Python?

We can access the enum members by using the name or value of the member items. In the below example we first access the value by name where we use the name of the enu as an index.

What is enum Auto ()?

Syntax : enum.auto() Automatically assign the integer value to the values of enum class attributes. Example #1 : In this example we can see that by using enum. auto() method, we are able to assign the numerical values automatically to the class attributes by using this method.

How do you convert enum to int in Python?

Use the IntEnum class from the enum module to convert an enum to an integer in Python. You can use the auto() class if the exact value is unimportant. To get a value of an enum member, use the value attribute on the member.


1 Answers

You can do the following:

[e.value for e in Color] 
like image 184
Ozgur Vatansever Avatar answered Sep 19 '22 22:09

Ozgur Vatansever