Skip to content

Instantly share code, notes, and snippets.

@pcperini
Last active October 13, 2015 05:48
Show Gist options
  • Save pcperini/4148848 to your computer and use it in GitHub Desktop.
Save pcperini/4148848 to your computer and use it in GitHub Desktop.
Categories in Python
# categories.py
class category(object):
def __init__(self, mainModule, override = True):
self.mainModule = mainModule
self.override = override
def __call__(self, function):
if self.override or function.__name__ not in dir(self.mainModule):
setattr(self.mainModule, function.__name__, function)
def categorize(module, function):
category(module).__call__(function)
##########
# categories_test.py
import this
from categories import category
@category(this)
def all():
print "all things are this"
this.all()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment