0

Is it possible to give a wrapped function the arg and kwargs names of the function it is wrapping? I need to do this because decorators that are applied later use the arg names of the underlying function.

def wrapper(func):
    def wrapped(<func args and kwargs names and values>):
        return func(*args, **kwargs)

So if the func been passed in was foo(x,y=3), the returned wrapped function would have the signature wrapped(x, y=3) instead of the usual wrapped(*args, **kwargs).

--Edit--

Just found that this is a duplicate of Preserving signatures of decorated functions . Thanks for the answers anyway

Community
  • 1
  • 1
Brendan Maguire
  • 4,188
  • 4
  • 24
  • 28

1 Answers1

0

I'm not sure I fully understand your question, isn't this what you expect ?

>>> def wrapper(func):
    def wrapped(*args, **kwargs):
        print args, kwargs
        return func(*args, **kwargs)
    return wrapped

>>> @wrapper
def foo(x, y=3):
    return x + y

>>> foo(3)
(3,) {}
6
>>> foo(3, 5)
(3, 5) {}
8
>>> foo(3, y=5)
(3,) {'y': 5}
8
Emmanuel
  • 13,935
  • 12
  • 50
  • 72