How to peek inside a decorated function

Peter Otten __peter__ at
Sun Feb 15 13:23:33 CET 2009

Steven D'Aprano wrote:

> Suppose I have a function f() which I know has been decorated, but I don't
> have access to the original undecorated function any longer:
> def reverse(func):
>     def f(*args):
>         args = list(args)
>         args.reverse()
>         return func(*args)
>     return f
> def say(*args):
>     print args
> rsay = reverse(say)
> del say
> Is there any way to peek inside the decorated function rsay() to get
> access to the undecorated function say()?

Here's a hack for Python 2.4:

def make_extractor(x=None):
    def extractor(): return x
    return extractor

extractor = make_extractor()
function = type(extractor)

def get_cell_contents(cell):
    return function(extractor.func_code, {}, "yadda", None, (cell,))()



More information about the Python-list mailing list