Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Get the name of the class of a decorated method in Python 2.6
def print_class_name(fn):
A decorator that prints the name of the class of a bound function (IE, a method).
This version works with Python 2.6.
NOTE: This MUST be the first decorator applied to the function! E.g.:
def my_fn(stuff):
This is because decorators replace the wrapped function's signature.
def inner(*args, **kwargs):
arg_spec = inspect.getargspec(fn)
# We assume that if a parameter named `self` exists for the wrapped
# function, the function is bound to a class, and we can get the name of
# the class from the function's first argument.
if 'self' in arg_spec.args:
cls = args[0].__class__
print 'Function bound to class %s' % cls.__name__
print 'Unbound function!'
return fn(*args, **kwargs)
return inner
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.