Skip to content

Instantly share code, notes, and snippets.

@mjg123
Created December 1, 2014 22:36
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 mjg123/6aad8e26306143dc4c8f to your computer and use it in GitHub Desktop.
Save mjg123/6aad8e26306143dc4c8f to your computer and use it in GitHub Desktop.
classes using a decorator to store methods will be dependent on decorator ordering
import pprint
import six
captured_methods = {}
class MjgClass(object):
@classmethod
def decorated(cls):
global captured_methods
def decorator(f):
print("Decorating: %s (already decorated: %s)" %
(f.__name__, getattr(f, "already_decorated", "no")))
captured_methods[f.__name__] = f
return f
return decorator
def another_decorator(f):
def y(self, x):
print("Running pre-decorated %s" % f.__name__)
f(self, x)
y.already_decorated = "yes"
y.__name__ = f.__name__
return y
class MjgSubclass(MjgClass):
@MjgClass.decorated()
@another_decorator
def mjg_method1(self, x):
print("X is %s" % x)
@another_decorator
@MjgClass.decorated()
def mjg_method2(self, x):
print("X is %s" % x)
m = MjgSubclass()
m.mjg_method1(4)
m.mjg_method2(3)
captured_methods['mjg_method1'](None, 4)
captured_methods['mjg_method2'](None, 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment