help with flexible decorators

Lawrence Oluyede raims at dot.com
Mon Aug 6 22:30:27 EDT 2007


james_027 <cai.haibin at gmail.com> wrote:
> While the enhance decorator work with functions of 1 argument, how do
> I make it to work with more than one arguments.

Using *args. Something like this:

def enhance(f):
    def _new(*args):
        return f(*args) + 1
    return _new

@enhance
def f(*args):
    return sum(args)

In [22]: f(6)
Out[22]: 7

In [23]: f(6, 4)
Out[23]: 11

In [24]: f(6, 4, 10)
Out[24]: 21


-- 
Lawrence, oluyede.org - neropercaso.it
"It is difficult to get a man to understand 
something when his salary depends on not
understanding it" - Upton Sinclair



More information about the Python-list mailing list