Skip to content

Instantly share code, notes, and snippets.

@blacktaxi
Created November 24, 2012 13:08
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 blacktaxi/4139621 to your computer and use it in GitHub Desktop.
Save blacktaxi/4139621 to your computer and use it in GitHub Desktop.
Class decorator for convenient creation of class attribute-based enumeration constants.
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