Skip to content

Instantly share code, notes, and snippets.

@GuillermoBlasco
Created January 2, 2014 22:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GuillermoBlasco/8228679 to your computer and use it in GitHub Desktop.
Save GuillermoBlasco/8228679 to your computer and use it in GitHub Desktop.
Simulation of enums for python 2.x
# Simulation of enums for python 2.x
class Enum(object):
def __init__(self, **kwargs):
for key, value in kwargs.iteritems():
setattr(self, key, value)
self._asDict = dict(kwargs)
def __contains__(self, item):
return item in self._asDict.values()
def __iter__(self):
return self._asDict.values().__iter__()
def __getitem__(self, key):
return self._asDict[key]
def __add__(self, other):
if not isinstance(other, Enum):
raise ValueError("Expected enum object to sum")
return Enum(**dict(self._asDict.items() + other._asDict.items()))
def __str__(self):
return str(self._asDict)
e1 = Enum(ONE = 1, TWO = 2)
e2 = Enum(THREE = 3)
print e1+e2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment