Created
December 1, 2014 22:36
-
-
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
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
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