decorating a function and adding functionalities preserving the number of argument

Posted by pygabriel on Stack Overflow See other posts from Stack Overflow or by pygabriel
Published on 2010-05-28T20:38:43Z Indexed on 2010/05/28 20:42 UTC
Read the original article Hit count: 202

Filed under:
|

I'd like to decorate a function, using a pattern like this:

def deco(func):
    def wrap(*a,**kw):
        print "do something"
        return func(*a,**kw)
    return wrap

The problem is that if the function decorated has a prototype like that:

def function(a,b,c): return

When decorated, the prototype is destroyed by the varargs, for example, calling function(1,2,3,4) wouldn't result in an exception. Is that a way to avoid that? How can define the wrap function with the same prototype as the decorated (func) one?

There's something conceptually wrong?

© Stack Overflow or respective owner

Related posts about python

Related posts about decorator