20.11.2024 - enum.txt

If you want to make an enum in python, you have to make an enum subclass and write something like:
from enum import Enum

class MyEnum(Enum):
    SOUP = 0
    SUSHI = 1
    PIZZA = 2
print(MyEnum.SOUP.value)
Can you still do it if you hate importing modules? and classes? Of course you can, as long as you believe that functions can be *anything*
def my_enum(): ...
for ind, s in enumerate(["SOUP", "SUSHI", "PIZZA"]):
    my_enum.__dict__[s] = ind
print(my_enum.SOUP)
This code is also one line shorter. What's not to like?

Go back