xxxxxxxxxx
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
#value of enum
print(Color.BLUE)
#type of enum
print(type(Color.BLUE))
#check if color Blue is in Color enum
print(Color.BLUE in Color)
#print all Colors
for color in Color:
print(color)
xxxxxxxxxx
from enum import Enum
class Status(Enum):
ACTIVE = 1
INACTIVE = 2
@classmethod
def has_value(cls, value):
return any(value == item.value for item in cls)
@classmethod
def has_key(cls, value):
return any(value == item.name for item in cls)
print(Status.has_value(1)) # True
print(Status.has_value(5)) # False
print(Status.has_key('ACTIVE')) # True
print(Status.has_key('TEST')) # False