Can I undecorate a function?

Diez B. Roggisch deets at nospam.web.de
Mon Jan 29 18:25:52 CET 2007


Matthew Wilson wrote:

> The decorator as_string returns the decorated function's value as
> string.  In some instances I want to access just the function f,
> though, and catch the values before they've been decorated.
> 
> Is this possible?
> 
> def as_string(f):
>     def anon(*args, **kwargs):
>         y = f(*args, **kwargs)
>         return str(y)
>     return anon
> 
> @as_string
> def f(x):
>     return x * x

Untested:


def as_string(f):
    def anon(*args, **kwargs):
        y = f(*args, **kwargs)
        return str(y)
    andon.the_function = f
    return anon


@as_string
def f(x):
   return x * x

print f.the_function(10)


Diez



More information about the Python-list mailing list