Python does not have any syntax for creating enums (it’s proposal was rejected), but there are some alternatives.
1. Using a class
class Numbers(object): ONE = 1 TWO = 2 THREE = 3 assert Numbers.ONE == 1 assert Numbers.TWO == 2 assert Numbers.THREE == 3
2. Use a function that dynamically creates a new class
def enum(**enums): return type('Enum', (object,), enums) Numbers = enum(ONE=1, TWO=2, THREE=3) assert Numbers.ONE == 1 assert Numbers.TWO == 2 assert Numbers.THREE == 3
3. Create a new object that extends Python’s set
class Enum(set): def __getattr__(self, name): if name in self: return name raise AttributeError Numbers = Enum( "ONE TWO THREE".split() ) error = False try: print Numbers.ONE print Numbers.TWO print Numbers.THREE except: error = True assert error == False try: print Numbers.FOUR except: error = True assert error == True