Skip to content

Instantly share code, notes, and snippets.

@nava45
Forked from satiani/gist:5796548
Created June 19, 2013 06:13
Show Gist options
  • Save nava45/5812030 to your computer and use it in GitHub Desktop.
Save nava45/5812030 to your computer and use it in GitHub Desktop.
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
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