help with flexible decorators

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Tue Aug 7 03:28:21 EDT 2007


james_027 a écrit :
> Hi,
> 
> I want to write a flexible decorators to edit a function that may have
> 1 or more arguments...
> 
> def enhance(func):
>     def new(x):
>         #do something ...
>         return func(x)
>     return new
> 
> @enhance
> def method_a(x):
>     #do something ...
> 
> While the enhance decorator work with functions of 1 argument, how do
> I make it to work with more than one arguments.

Use *args (for positional args) and **kw (for keyword args):

def enhance(func):
     def new(*args, **kw):
         #do something ...
         return func(*args, **kw)
     return new

@enhance
def method_a(x):
     #do something ...


HTH



More information about the Python-list mailing list