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?
The Enum type comes from languages like C and C++. The motivation is explained e.g. here:
An enum is a user-defined type consisting of a set of named constants called enumerators. The idea is that instead of using an int to represent a set of values, a type with a restricted set of values is used instead. ... The problem with [using integers for representing rainbow colors] is that there are many more ints than colors. If violet has the value 7, and the program assigns a value of 15 to a variable, then it is clearly a bug - but might not be detected, as 15 is a valid value for an int. ... Using enums increase the level of abstraction and lets the programmer think about what the values mean rather than worry about how they are stored and accessed. This reduces the occurrence of bugs.
Enums have these benefits:
- They restrict the values that the enum variable can take.
- They force you to think about all the possible values that the enum can take.
- They are a constant rather than a number, increasing readability of the source code
Traffic lights are a nice example:
from enum import Enum
class TrafficLights(Enum):
red = 4 # 4 is the bit representation
red_yellow = 6
green = 1
yellow = 2
print(TrafficLights.yellow)
print(TrafficLights(2))
print(repr(TrafficLights.yellow))
# Looking at object attributes
print(dir(TrafficLights.yellow))
# Accessing value by name
print(TrafficLights.yellow.value)
for light in TrafficLights:
print(light)
Note that the traffic lights have a particular bit representation, and cannot take any other values. Also, the class is iterable and the items can be accessed by name or by value, which is not possible with regular attributes.
Bottom line, enum is not an essential module, but is nice to have. It can be useful for specific use cases, where a limited set of enumerated objects is required.
From the documentation
An enumeration is a set of symbolic names (members) bound to unique, constant values
The keywords are bound to unique and constant values.
Say you have
>>> class Color(Enum):
... RED = 1
... GREEN = 2
... BLUE = 3
and
>>> class ColorClass():
... RED = 1
... GREEN = 2
... BLUE = 3
You are allowed to do this
>>> ColorClass.RED = 3
>>>
But when you do
>>> Color.RED = 3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\Programme\Python37\lib\enum.py", line 386, in __setattr__
raise AttributeError('Cannot reassign members.')
AttributeError: Cannot reassign members.
you get
AttributeError: Cannot reassign members.
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