Tony Tony -3 years ago 115
Python Question

Introspection to get decorator names on a method?

I am trying to figure out how to get the names of all decorators on a method. I can already get the method name and docstring, but cannot figure out how to get a list of decorators.

Answer Source

If you can change the way you call the decorators from

class Foo(object):
    def bar(self):


class Foo(object):
    def bar(self):

then you could register the decorators this way:

def register(*decorators):
    def register_wrapper(func):
        for deco in decorators[::-1]:
        return func
    return register_wrapper

For example:

def many(f):
    def wrapper(*args,**kwds):
        return f(*args,**kwds)
    return wrapper

decos = here = many

class Foo(object):
    def bar(self):


Here we access the tuple of decorators:

# (<function many at 0xb76d9d14>, <function decos at 0xb76d9d4c>, <function here at 0xb76d9d84>)

Here we print just the names of the decorators:

print([d.func_name for d in])
# ['many', 'decos', 'here']
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download