Created
November 29, 2013 07:39
-
-
Save kanghyojun/7702636 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| from functools import wraps | |
| class R(object): | |
| def __init__(self, a): | |
| self.a = a | |
| def post(self, f): | |
| setattr(self.a, 'blah', 'blah') | |
| class A(object): | |
| def reg(self, f): | |
| self.f = f | |
| setattr(self, f.func_name, R(self)) | |
| @wraps(f) | |
| def deco(*args, **kwards): | |
| print 'called' | |
| setattr(f, 'a', 'a') | |
| return f(*args, **kwards) | |
| return deco | |
| a = A() | |
| @a.reg | |
| def some_func(): | |
| print 'a' | |
| @a.some_func.post | |
| def post_func(): | |
| print 'b' | |
| if __name__ == '__main__': | |
| print a.f | |
| print a.some_func | |
| print a.blah |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment