Created
November 13, 2012 19:18
-
-
Save nickelpro/4067780 to your computer and use it in GitHub Desktop.
Do-It-Yourself Enumeration in Python
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
class enum(object): | |
def __init__(self, *names): | |
for number, name in enumerate(names): | |
setattr(self, name, number) | |
Example: | |
Month = enum( | |
"January", | |
"Febuary", | |
"March", | |
... | |
"December", | |
) | |
print Month.January | |
print Month.March | |
print Month.December | |
Output: | |
0 | |
3 | |
11 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Gratefully copied from SO, just added the asterisk to allow for argument lists
http://stackoverflow.com/questions/702834/whats-the-common-practice-for-enums-in-python