Skip to main content
Update for python 3.x
Source Link

Keep it simple, using old Python 2.x (see below for Python 3!):

class Enum(object): def __init__(self, tupleList): self.tupleList = tupleList def __getattr__(self, name): return self.tupleList.index(name) 

Then:

DIRECTION = Enum(('UP', 'DOWN', 'LEFT', 'RIGHT')) DIRECTION.DOWN 1 

Keep it simple when using Python 3:

from enum import Enum class MyEnum(Enum): UP = 1 DOWN = 2 LEFT = 3 RIGHT = 4 

Then:

MyEnum.DOWN 

See: https://docs.python.org/3/library/enum.html

Keep it simple:

class Enum(object): def __init__(self, tupleList): self.tupleList = tupleList def __getattr__(self, name): return self.tupleList.index(name) 

Then:

DIRECTION = Enum(('UP', 'DOWN', 'LEFT', 'RIGHT')) DIRECTION.DOWN 1 

Keep it simple, using old Python 2.x (see below for Python 3!):

class Enum(object): def __init__(self, tupleList): self.tupleList = tupleList def __getattr__(self, name): return self.tupleList.index(name) 

Then:

DIRECTION = Enum(('UP', 'DOWN', 'LEFT', 'RIGHT')) DIRECTION.DOWN 1 

Keep it simple when using Python 3:

from enum import Enum class MyEnum(Enum): UP = 1 DOWN = 2 LEFT = 3 RIGHT = 4 

Then:

MyEnum.DOWN 

See: https://docs.python.org/3/library/enum.html

typo fix
Source Link

Keep it simple:

class Enum(object): def __init__(self, tupleList): self.tupleList = tupleList def __getattr__(self, name): return self.tupleList.index(name) 

ThanThen:

DIRECTION = Enum(('UP', 'DOWN', 'LEFT', 'RIGHT')) DIRECTION.DOWN 1 

Keep it simple:

class Enum(object): def __init__(self, tupleList): self.tupleList = tupleList def __getattr__(self, name): return self.tupleList.index(name) 

Than:

DIRECTION = Enum(('UP', 'DOWN', 'LEFT', 'RIGHT')) DIRECTION.DOWN 1 

Keep it simple:

class Enum(object): def __init__(self, tupleList): self.tupleList = tupleList def __getattr__(self, name): return self.tupleList.index(name) 

Then:

DIRECTION = Enum(('UP', 'DOWN', 'LEFT', 'RIGHT')) DIRECTION.DOWN 1 
Source Link

Keep it simple:

class Enum(object): def __init__(self, tupleList): self.tupleList = tupleList def __getattr__(self, name): return self.tupleList.index(name) 

Than:

DIRECTION = Enum(('UP', 'DOWN', 'LEFT', 'RIGHT')) DIRECTION.DOWN 1 
Post Made Community Wiki by Melroy van den Berg