recursive decorator

Michele Simionato michele.simionato at gmail.com
Sat Sep 5 06:25:07 CEST 2009


On Sep 4, 7:03 pm, Ethan Furman <et... at stoneleaf.us> wrote:

> Just to verify, using the decorator module is portable, yes?

Yes, it is portable. BTW, here is what you want to do (requires
decorator-3.1.2):

from decorator import FunctionMaker

def bindfunc(f):
    name = f.__name__
    signature = ', '.join(FunctionMaker(f).args[1:]) # skip first arg
    return FunctionMaker.create(
        '%s(%s)' % (name,  signature),
        'return _func_(%s, %s)' % (name, signature),
        dict(_func_=f), defaults=f.func_defaults,
               doc=f.__doc__, module=f.__module__)

@bindfunc
def fac(self, n):
    return 1 if n <= 1 else n * self(n - 1)

print fac(5)
help(fac)



More information about the Python-list mailing list