As I am learning about the concepts in Python I came across this enum module. I tried the below code:
import enum
# Using enum class create enumerations
class Days(enum.Enum):
Sun = 1
Mon = "raj"
Tue = 3
Wed= 123
Thur=12312312
Fri=1312312
Sat=12122135435
# print the enum member as a string
print("The enum member as a string is : ",end="")
print(Days.Mon)
# print the enum member as a repr
print("he enum member as a repr is : ",end="")
print(repr(Days.Sun))
print(repr(Days.fri))
# Check type of enum member
print("The type of enum member is : ",end ="")
print(type(Days.Mon))
# Accessing enums by names or values
print(Days['Mon'])
print(Days(3))
print(Days['thur'])
print(Days['thur'] == "thur")
print(Days(12122135435) == "sat" )
# print name of enum member
print("The name of enum member is : ",end ="")
print(Days.Tue.name)
Output:
Days.Mon
he enum member as a repr is : <Days.Sun: 1>
<Days.fri: 1312312>
The type of enum member is : <enum 'Days'>
Days.Mon
Days.Tue
Days.thur
False
False
The name of enum member is: Tue
Below is simple class and class variable access:
class Raj:
yoo = 1
boo = "raj"
too = 213
print(Raj.yoo)
print(Raj.boo)
print(Raj.too)
class Materials:
Shaded, Shiny, Transparent, Matte = range(1, 5)
print(Materials.Matte)
print(Materials.Transparent)
Output:
1
raj
213
4
3
I am not able to understand what is the use case of enum when we can simply use class variables and call them simply to get values. The enum function just returns what we ask for print and return the name if we call by value. As you can all see in the above code I tried to match the return Days['thur'] == "thur"
I thought by calling like this it is returning the name but nope. So tried calling like Days(12122135435) == "sat"
this so I can get the name of the value and can match. But nope.
I am very much confused about why it is defined and why it is used?