Course
Enums
Python Tutorial
This Python tutorial has been written for the beginners to help them understand the basic to advanced concepts of Python Programming Language. After completing this tutorial, you will find yourself at a great level of expertise in Python, from where you can take yourself to the next levels to become a world class Software Engineer.
Enums
The term '
enumeration
' refers to the process of assigning fixed constant values to a set of strings, so that each string can be identified by the value bound to it. Python's standard library offers the enum
module. The Enum class included in enum
module is used as the parent class to define enumeration of a set of identifiers − conventionally written in upper case.Example 1
from enum import Enum
class subjects(Enum): ENGLISH = 1 MATHS = 2 SCIENCE = 3 SANSKRIT = 4
In the above code, "subjects" is the enumeration. It has different enumeration members, e.g., subjects.MATHS. Each member is assigned a value.
Each member is ab object of the enumeration class subjects, and has name and value attributes.
obj = subjects.MATHSprint (type(obj), obj.value)
It results in following output
<enum 'subjects'> 2
Example 2
Value bound to the enum member needn't always be an integer, it can be a string as well. See the following example
from enum import Enum
class subjects(Enum): ENGLISH = "E" MATHS = "M" GEOGRAPHY = "G" SANSKRIT = "S" obj = subjects.SANSKRITprint (type(obj), obj.name, obj.value)
It will produce the following output
<enum 'subjects'> SANSKRIT S
Example 3
You can iterate through the enum members in the order of their appearance in the definition, with the help of a
for
loopfor sub in subjects: print (sub.name, sub.value)
It will produce the following output
ENGLISH EMATHS MGEOGRAPHY GSANSKRIT S
The enum member can be accessed with the unique value assigned to it, or by its name attribute. Hence, subjects("E") as well as subjects["ENGLISH"] returns subjects.ENGLISH member.
Example 4
An enum class cannot have same member appearing twice, however, more than one members may be assigned same value. To ensure that each member has a unique value bound to it, use the
@
unique
decorator.from enum import Enum, unique
@uniqueclass subjects(Enum): ENGLISH = 1 MATHS = 2 GEOGRAPHY = 3 SANSKRIT = 2
This will raise an exception as follows −
@unique ^^^^^^ raise ValueError('duplicate values found in %r: %s' %ValueError: duplicate values found in <enum 'subjects'>: SANSKRIT -> MATHS
The Enum class is a callable class, hence you can use the following alternative method of defining enumeration
from enum import Enumsubjects = Enum("subjects", "ENGLISH MATHS SCIENCE SANSKRIT")
The
Enum
constructor uses two arguments here. First one is the name of enumeration. Second argument is a string consisting of enumeration member symbolic names, separated by a whitespace.Practice with Online Editor
Note: This Python online Editor is a Python interpreter written in Rust, RustPython may not fully support all Python standard libraries and third-party libraries yet.
Remember to save code(Ctrl
+S
Or
Command
+S
) before run it.