RED, GREEN, YELLOW = range(3) print(RED) print(GREEN) print(YELLOW) from enum import Enum class Day(Enum): MONDAY = 1 TUESDAY = 2 WEDNESDAY = 3 THURSDAY = 4 FRIDAY = 5 SATURDAY = 6 SUNDAY = 7 print(list(Day)) print(type(Day.MONDAY)) print(Day.FRIDAY.name) print(Day.FRIDAY.value) from enum import Enum class Season(Enum): WINTER, SPRING, SUMMER, FALL = range(1, 5) print(list(Season)) from enum import Enum class Size(Enum): S = "small" M = "medium" L = "large" XL = "extra large" print(list(Size)) from enum import Enum class SwitchPosition(Enum): ON = True OFF = False print(list(SwitchPosition)) from enum import Enum class UserResponse(Enum): YES = 1 NO = "No" print(list(UserResponse)) from enum import Enum HTTPMethod = Enum( "HTTPMethod", ["GET", "POST", "PUSH", "PATCH", "DELETE"] ) print(list(HTTPMethod)) from enum import Enum class HTTPMethod(Enum): GET = 1 POST = 2 PUSH = 3 PATCH = 4 DELETE = 5 print(list(HTTPMethod)) from enum import Enum HTTPStatusCode = Enum( value="HTTPStatusCode", names=[ ("OK", 200), ("CREATED", 201), ("BAD_REQUEST", 400), ("NOT_FOUND", 404), ("SERVER_ERROR", 500), ], ) print(list(HTTPStatusCode)) from enum import auto, Enum class Day(Enum): MONDAY = auto() TUESDAY = auto() WEDNESDAY = 3 THURSDAY = auto() FRIDAY = auto() SATURDAY = auto() SUNDAY = 7 print(list(Day)) from enum import Enum, auto class CardinalDirection(Enum): def _generate_next_value_(name, start, count, last_values): return name[0] NORTH = auto() SOUTH = auto() EAST = auto() WEST = auto() print(list(CardinalDirection)) from enum import Enum class Flavor(Enum): VANILLA = 1 CHOCOLATE = 2 MINT = 3 for flavor in Flavor: print(flavor) for flavor in Flavor: print(flavor.name, "->", flavor.value) from enum import Enum class Semaphore(Enum): RED = 1 YELLOW = 2 GREEN = 3 def handle_semaphore(light): if light is Semaphore.RED: print("You must stop!") elif light is Semaphore.YELLOW: print("Light will change to red, be careful!") elif light is Semaphore.GREEN: print("You can continue!") handle_semaphore(Semaphore.GREEN) handle_semaphore(Semaphore.YELLOW) handle_semaphore(Semaphore.RED) """ from enum import Enum class Semaphore(Enum): RED = 1 YELLOW = 2 GREEN = 3 def handle_semaphore(light): match light: case Semaphore.RED: print("You must stop!") case Semaphore.YELLOW: print("Light will change to red, be careful!") case Semaphore.GREEN: print("You can continue!") handle_semaphore(Semaphore.GREEN) handle_semaphore(Semaphore.YELLOW) handle_semaphore(Semaphore.RED) """ from enum import Enum class AtlanticAveSemaphore(Enum): RED = 1 YELLOW = 2 GREEN = 3 PEDESTRIAN_RED = 1 PEDESTRIAN_GREEN = 3 red = AtlanticAveSemaphore.RED print(red is AtlanticAveSemaphore.RED) print(red is not AtlanticAveSemaphore.RED) yellow = AtlanticAveSemaphore.YELLOW print(yellow is red) print(yellow is not red) pedestrian_red = AtlanticAveSemaphore.PEDESTRIAN_RED print(red is pedestrian_red) from enum import Enum class Sort(Enum): ASCENDING = 1 DESCENDING = 2 def __call__(self, values): return sorted(values, reverse=self is Sort.DESCENDING) numbers = [5, 2, 7, 6, 3, 9, 8, 4] print(Sort.ASCENDING(numbers)) print(Sort.DESCENDING(numbers)) from enum import IntFlag class Role(IntFlag): OWNER = 8 POWER_USER = 4 USER = 2 SUPERVISOR = 1 ADMIN = OWNER | POWER_USER | USER | SUPERVISOR john_roles = Role.USER | Role.SUPERVISOR print(john_roles) print(john_roles.value)