Created
November 24, 2012 13:08
-
-
Save blacktaxi/4139621 to your computer and use it in GitHub Desktop.
Class decorator for convenient creation of class attribute-based enumeration constants.
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_class(cls): | |
"""Decorates a class to set it's attributes to values of their | |
literal names. This is cool to use to make an 'enum' class and | |
have PyCharm also infer it's members. | |
Use it like so: | |
>>> @enum_class | |
... class KindOfFruit: | |
... Apple, Orange, Banana, Passionfruit = range(4) | |
... | |
>>> orange_kind = KindOfFruit.Orange | |
>>> print orange_kind | |
Orange | |
""" | |
for attr in dir(cls): | |
if not attr.startswith('__'): | |
setattr(cls, attr, attr) | |
return cls |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment