Skip to content

Instantly share code, notes, and snippets.

@subramaniank
Created March 2, 2015 10:26
Show Gist options
  • Save subramaniank/2005627003ae30846db2 to your computer and use it in GitHub Desktop.
Save subramaniank/2005627003ae30846db2 to your computer and use it in GitHub Desktop.
Enum in python. Sequential and named enumerations with reverse_mapping(stable, given that enum values are unique)
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment