Last active
August 29, 2015 14:20
-
-
Save lsloan/ae43a070b9d0ee500763 to your computer and use it in GitHub Desktop.
Pure Python implementation of an enumerated type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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