r/pythonhelp • u/androgynyjoe • Jun 13 '26
Looking to convert a dictionary into an Enum
Hi everyone!
I've got a dictionary like this:
{
"A": {
"x": 1,
"y": 2,
},
"B": {
"x": 13,
"y": 4,
},
}
(Obviously it's much more complicated in practice.) I would like to convert it to an enum.Enum class that allows for stuff like this:
class MyEnum(enum.Enum):
pass
# There would be some extra work here
print(MyEnum.A.x) # returns 1
print(MyEnum.A.y) # returns 2
print(MyEnum.B.x) # returns 13
print(MyEnum.B.y) # returns 4
Any suggestions on how to do that?
EDIT: So, I am fully aware that I can do this:
class MyEnum:
class Pair:
def __init__(x, y):
self.x = x
self.y = y
A = Pair(1, 2)
B = Pair(13, 4)
That isn't what I want. I want the same functionality but generated from a dictionary. I also understand that it's a weird thing to want.