Module Contents of Enum Class:
There are four module content or Enumeration classes that are used to define unique sets of values and names. The four enumeration classes are IntFlag, Flag, IntEnum, and Enum. Moreover, it also provides a helper, auto, and a decorator, Unique(). Enum class is created as ‘class enum.Enum’. It is a base class that is utilized to create enumerated constants. IntEnum class is created as ‘class enum.IntEnum’. The IntEnum enumeration class is another base class used to create enumerated constants, the subclasses of int. The next class is Flag, which is created as ‘class enum.Flag’.
Flag creates the enumerated constants that can be combined by using bitwise operations without losing the Flag membership. The IntFlag enumeration class has the same functionality as the Flag enumeration class. It also creates enumerated constants that bitwise operators can combine without losing the IntFlag membership. The decorator, unique() is created as ‘enum.unique()’, which ensures that only value is associated with one name. Last but not least, the helper auto is created as ‘class enum.auto’. It is used to replace the instances of Enum members with a suitable value. However, the starting value begins at 1, by default.
Our first example is about creating the enum class. As it is observed that Enums are created using the class syntax, which makes it easy to write and read. Enums are extremely helpful where one variable can acquire one of a partial collection of values. Here is an instance of how to define an Enum class:
JANUARY = 1
FEBURARY = 2
MARCH = 3
APRIL = 4
MAY = 5
JUNE = 6
Notice that creating an Enum class itself is very simple, just like writing a class that is inherited from Enum itself. Moreover, the value of an Enum’s member may or may not be essential; however, either way, the member’s value is used to extract the corresponding member. Look at the example given below:
When you call the member of Enum ‘Months’, you will get the following output:
The other way to get the Enum’s member is shown in the example below:
Here is the output of the above statement.
As you can see, both ways you can get the members of Enum class.
Note: the values of Enum members could be anything; str, int, etc. when the exact value is not important, the instances of helper ‘auto’ can be used so that a suitable value can be chosen. However, you need to be careful while mixing ‘auto’ helper with other values.
You can observe that creating and defining Enum is very simple, just like creating or defining any other class. However, here we have a simple example to help you understand converting python string to Enum. Let us run the code given below and see what we get after executing it:
PURCHASE = 50000
SALE = 10000
Copy-paste the code or make your own customized Enum class by simply following the directions and seeing the output. Enter the subsequent command to convert the python string to enum.
The following output will be produced when you enter the code given above:
Another example of converting the python string to Enum is given below:
x = 'abc'
y = 456
When you execute the above statements, you will get the following output:
As you can see, the string is converted to both str() and int(). The value of Enum member can be extracted in either way.
This article uses the correct way to convert the python string to enum. First, we have discussed what Enum is and how it can be used. There are four classes of Enumeration; Enum, IntEnum, Flag, IntFlag, and there is one helper, ‘auto’, and one decorator, unique(). Enumeration classes are bases classes used to create enumerated constants. The helper ‘auto’ is used to replace the instances of Enum members with a suitable value. While the decorator, unique() is used to ensure that only one value is allocated to a name. After discussing the enumeration classes, we have learned how to create an Enum class and how it can be used for python string to enum conversion. This article will help you understand the python string conversion to Enum and will guide you to make your own Enum classes without making any mistakes. Follow the directions and start making your enum classes for python string to enum conversion.