Sunday 12 September 2021

Python: IntEnum: behave like integer

IntEnum values behave like integers and integer enumerations of different types can also be compared to each other.

 

IntEnum.py

from enum import IntEnum

class PrimeNumbers(IntEnum):
    TWO = 2
    THREE = 3
    FIVE = 5
    SEVEN = 7

class EvenNumbers(IntEnum):
    TWO = 2
    FOUR = 4
    SIX = 6
    EIGHT = 8

class OddNumbers(IntEnum):
    ONE = 1
    THREE = 3
    FIVE = 5
    SEVEN = 7


print('PrimeNumbers.TWO == 2', ' -> ', PrimeNumbers.TWO == 2)
print('PrimeNumbers.TWO == EvenNumbers.TWO', ' -> ', PrimeNumbers.TWO == EvenNumbers.TWO)
print('PrimeNumbers.TWO == OddNumbers.ONE', ' -> ', PrimeNumbers.TWO == OddNumbers.ONE)

 

Output

PrimeNumbers.TWO == 2  ->  True
PrimeNumbers.TWO == EvenNumbers.TWO  ->  True
PrimeNumbers.TWO == OddNumbers.ONE  ->  False

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment