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?
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.
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.
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.
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.
You can do the following:
[e.value for e in Color]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With