Python : All About Enumeration Object

from enum import IntEnum, Enum, unique
import sys
sys.stdout = open(__name__, 'w')

class CountryCode1(Enum):
    Nepal = 977    India = 91    Pakistan = 92    Bangaladesh = 90    Bhutan = 93    Bhutan = 90

class CountryCode2(IntEnum):
    Nepal = 977    India = 91    Pakistan = 92    Bangaladesh = 90    Bhutan = 93    Bhutan = 90

def test_countryCode_1():
    for i in CountryCode1:
        print("With enum Inherited from Enum  => Country : {}, Code : {}".format(i.name, i.value))

def test_countryCode_2():
    try:
        for i in sorted(CountryCode1):
            print("With enum Inherited from Enum & Trying to Sort Enum Object  => Country : {}, Code : {}".format(i.name,
                                                                                                              i.value))
    except Exception as e:
        print("With enum Inherited from Enum & Trying to Sort Enum Object: {}".format(e))

def test_countryCode_3():
    for i in sorted(CountryCode2):
        print("With enum Inherited from IntEnum & Trying to Sort Enum Object  => Country : {}, Code : {}".format(
            i.name, i.value))
test_countryCode_1() # It prints The Enum Member and values as expected.
test_countryCode_3() # It prints The Enum Member and values as expected.
test_countryCode_2() # It Handles the TypeError exception On Sorting as there is no key as value of Enum Object.

@uniqueclass CountryCode3(IntEnum):
    Nepal = 977    India = 91    Pakistan = 92    Bangaladesh = 90    Bhutan = 93    Bhutan = 90
def test_countryCode_4():
    for i in sorted(CountryCode3):
        print("With enum Inherited from IntEnum & unique decorator => Country : {}, Code : {}".format(
            i.name, i.value))
test_countryCode_4() # The program itself halts due to Enum Object CountryCode3 is not initialized due to unique
# decorator.

Comments