Skip to content

Instantly share code, notes, and snippets.

@lsloan
Last active August 29, 2015 14:20
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 lsloan/ae43a070b9d0ee500763 to your computer and use it in GitHub Desktop.
Save lsloan/ae43a070b9d0ee500763 to your computer and use it in GitHub Desktop.
Pure Python implementation of an enumerated type
def enum(*sequential, **named):
enums = dict(zip(sequential, range(len(sequential))), **named)
reverse = dict((value, key) for key, value in enums.iteritems())
enums['reverse_mapping'] = reverse
return type('Enum', (), enums)
def example():
Numbers = enum(ONE=1, TWO=2, THREE='three')
print Numbers.ONE
print Numbers.THREE
print Numbers.reverse_mapping['three']
if (__name__ == '__main__'):
example()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment