I was thinking about making a decorator for the purpose of increasing performance. A decorator that modifies the source code of the function it decorates, and returns the modified function.
While thinking this through, I figured that if I could just get the source code of the function, I could do this. But is it possible to access the source code of a function inside a decorator? If I have a decorator like this:
import inspect
def decorate(f):
exec(inspect.getsource(f))
return eval(f.__name__)
@decorate
def test():
return 1
I get an OSError:
OSError: could not get source code
This appears to be because test
is not fully formed before it is passed into decorate
. However, this works:
import inspect
def decorate(f):
exec(inspect.getsource(f))
return eval(f.__name__)
def test():
return 1
test = decorate(test)
It just doesn't have that decorator flair to it, though. It seems that this might be possible, because f.__code__
is defined.
Upon further inspection, it appears that this only happens when I put the inspect.getsource(f)
into exec
. Otherwise, it seems that I can get the source code.
As a rough sketch of the first thing that's on my mind, I'm thinking of tail-recursion. I wrote this decorator that is unfortunately slow and requires a very specific style of writing the function to be decorated:
def tail_recurse(acc_default):
def decorate(f):
def wrapper(*args, acc=acc_default):
args = args + (acc,)
while True:
return_type, *rargs = f(*args)
if return_type is None:
return rargs[-1]
args = rargs
return wrapper
return decorate
Basically, I'm thinking of doing something as simple as replacing the body of a function with:
while True:
__body__
update_args
exec(inspect.getsource(f))
and it will work. Interesting!!