Python Enums: Named Constants for Improved Code Clarity

thumb_up 1  ·  sell Python enums, Enumerations in Python, Named constants for code clarity in Python

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.MATHS print (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.SANSKRIT print (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 loop −

for sub in subjects: print (sub.name, sub.value)

It will produce the following output −

ENGLISH E
MATHS M
GEOGRAPHY G
SANSKRIT 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 @unique class 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 Enum subjects = 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.

 

 

The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.

Was this answer helpful?

Related Articles

description

Exploring Python's Key Characteristic

Python is a feature rich high-level, interpreted, interactive and object-oriented scripting language. This tutorial will list down some of…

arrow_forward
description

Comparing Python and C++

Both Python and C++ are among the most popular programming languages. Both of them have their advantages and disadvantages. In this…

arrow_forward
description

Creating a Python Hello World Program

This tutorial will teach you how to write a simple Hello World program using Python Programming language. This program will make use of…

arrow_forward
description

Python's Versatile Application Domains

Python is a general-purpose programming language. It is suitable for development of wide range of software applications. Over last few…

arrow_forward
description

Understanding the Python Interpreter

Python is an interpreter-based language. In a Linux system, Python's executable is installed in /usr/bin/ directory. For Windows, the…

arrow_forward
arrow_back « Back