Enumerations in python


What’s the best way to implement an ‘enum’ in Python?

Whats the recognised way of doing enumerations in python?

For example, at the moment I’m writing a game and want to be able to move “up”, “down”, “left” and “right”. I’m using strings because I haven’t yet figured out how enumerations work in python, and so my logic is littered with things like this:

def move(self, direction):
    if direction == "up":
        # Do something

I want to replace "up" with something like Directions.up


class Directions:
    up = 0
    down = 1
    left = 2
    right =3

Answered By – Kugel

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published