Get The Most Affordable Hosting in the World!

Starting at just $1.87/month, Vercaa offers unbeatable pricing for world-class web hosting services.

Fast, reliable, and secure hosting to power your website without breaking the bank. Plus, enjoy a free CDN for faster loading times worldwide!

Get Started Now!

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.

Ця відповідь Вам допомогла? 1 Користувачі, які знайшли це корисним (1 Голосів)