Reputation: 2836
I created a decorator to print the name of the function it decorates and it works:
>>> def debug(func):
... msg=func.__qualname__
... def wrapper(*args, **kwargs):
... print(msg)
... return func(*args, **kwargs)
... return wrapper
...
>>> @debug
... def add(x, y):
... return x+y
...
>>> add(1,2)
add
3
Now I wanted to apply the wraps decorator to the wrapper but when I did I got the error "TypeError: update_wrapper() got multiple values for argument 'wrapped'"
>>> from functools import wraps
>>>
>>> def debug(func):
... msg=func.__qualname__
... @wraps
... def wrapper(*args, **kwargs):
... print(msg)
... return func(*args, **kwargs)
... return wrapper
...
>>> @debug
... def add(x, y):
... return x+y
...
>>> add(1,2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: update_wrapper() got multiple values for argument 'wrapped'
>>>
What I'm doing wrong and why the error occurs?
Upvotes: 8
Views: 2166
Reputation: 2836
Got it. Sorry, the issue was that I forgot to pass 'func' to the wraps decorator. Here is the correct code:
def debug(func):
msg = func.__qualname__
@wraps(func)
def wrapper(*args, **kwargs):
print(msg)
return func(*args, **kwargs)
return wrapper
Upvotes: 12