oursolutionarchitectoursolutionarchitect
  • Python Questions and Answers
  • Python - Programming Examples
  • Python - Quick Guide
  • Python - Useful Resources
  • Python - Discussion
    • Selected Reading
    • Q&A

    Python - 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.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.