Can I undecorate a function?

M

Matthew Wilson

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

Matt
 
D

Diez B. Roggisch

Matthew said:
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
 
S

skip

Matt> In some instances I want to access just the function f, though,
Matt> and catch the values before they've been decorated.

def f(x):
return x * x

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

Call the one you want.

Skip
 
K

Kent Johnson

Matt> In some instances I want to access just the function f, though,
Matt> and catch the values before they've been decorated.

def f(x):
return x * x

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

or just
fs = as_string(f)

Kent
 
S

Steve Holden

Matthew said:
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
It's much simpler not to use the decorator syntax if you also want
access to the original function.

Simply declare f as a function, then assign decorator(f) to some other name.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Blog of Note: http://holdenweb.blogspot.com
See you at PyCon? http://us.pycon.org/TX2007
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top