Skip to content

Instantly share code, notes, and snippets.

@pcperini
Last active May 1, 2016 18:29
Show Gist options
  • Save pcperini/a9bcb33ca0cfb8b56a5c493b0325295c to your computer and use it in GitHub Desktop.
Save pcperini/a9bcb33ca0cfb8b56a5c493b0325295c to your computer and use it in GitHub Desktop.
def Enum(**kwargs):
values = kwargs
class EnumClass(type):
def __new__(cls, name, parents, dct):
# create a class_id if it's not specified
if 'class_id' not in dct:
dct['class_id'] = name.lower()
# we need to call type.__new__ to complete the initialization
new_type = super(EnumClass, cls).__new__(cls, name, parents, dct)
# set the values. assumes new_type.__init__(superclass_value)
for value_name, value in values.items():
setattr(cls, value, new_type(value))
return new_type
return EnumClass
class Action(str):
__metaclass__ = Enum(
Store="store",
Append="append"
)
print Action.Store, type(Action.Store)
# >>> "store", <class '__main__.Action'>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment