Created
June 17, 2013 12:36
-
-
Save satiani/5796548 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 inspect import getmembers, getargspec, ismethod | |
from functools import wraps | |
from decorator import decorator | |
# Some super basic decorators | |
def std_decorator(f): | |
def std_wrapper(*args, **kwargs): | |
return f(*args, **kwargs) | |
return std_wrapper | |
def wraps_decorator(f): | |
@wraps(f) | |
def wraps_wrapper(*args, **kwargs): | |
return f(*args, **kwargs) | |
return wraps_wrapper | |
@decorator | |
def signature_preserving_decorator(f, *args, **kwargs): | |
return f(*args, **kwargs) | |
# A simple class with example decorators used | |
class SomeClass(object): | |
def method_one(self, x, y): | |
pass | |
@std_decorator | |
def method_two(self, x, y): | |
pass | |
@wraps_decorator | |
def method_three(self, x, y): | |
pass | |
@signature_preserving_decorator | |
def method_four(self, x, y): | |
pass | |
obj = SomeClass() | |
for name, func in getmembers(obj, predicate=ismethod): | |
print "Bound Name: %s" % name | |
print "Func Name: %s" % func.func_name | |
print "Args: %s" % getargspec(func)[0] | |
# Output | |
Bound Name: method_four | |
Func Name: method_four | |
Args: ['self', 'x', 'y'] | |
Bound Name: method_one | |
Func Name: method_one | |
Args: ['self', 'x', 'y'] | |
Bound Name: method_three | |
Func Name: method_three | |
Args: [] | |
Bound Name: method_two | |
Func Name: std_wrapper | |
Args: [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment