[Python-Dev] Multiple decorators per line (original) (raw)

Clark C. Evans cce at clarkevans.com
Mon Aug 23 21:09:53 CEST 2004


It seems that decorators solve two problems:

While J2 seems nice, I'd rather see someone build a 'composite' decorator as the function it really is, rather than introducing a new keyword:

def composite(instance):
    return decorator_a(decorator_b(instance))
    
def @composite func(args):
    """ function body """

By splitting these problems, the definition of the decorator can be on the same line as the 'def' line, and the composite decorator can be applied to more than one function. Also, the operation of the decorator is very obvious ... far less "deep magic".

One could even use function arguments...

def makeDecorator(use_a = False, use_b = True):
    retval = lambda instance: instance
    if use_a:
        retval = lambda instance: retval(decorator_a(instance))
    if use_b:
        retval = lambda instance: retval(decorator_b(instance))
    return retval

composite = makeDecorator(use_a = True)
def composite func(args):
    """ function body """

I suppose if one wanted more magic...

def @makeDecorator(use_a = True) func(args):
    """ function body """

In short, I don't see a reason why this solution needs to solve #2, as regular functions should suffice. Also, The problem is just #1

Cheers,

Clark



More information about the Python-Dev mailing list