+ 2

Help in Python decorator

What do "functools.wrap" in Line 2 ? can explaine it 🙏 def decorator(func): @functools.wraps(func) def wrapper_decorator(*args, **kwargs): # Do something before value = func(*args, **kwargs) # Do something after return value return wrapper_decorator

22nd Apr 2022, 7:15 PM
Amir
Amir - avatar
4 Réponses
+ 4
It keeps the specific attributes of func that will normally be shadowed by the wrapper. Eg the name of func
22nd Apr 2022, 8:10 PM
Oma Falk
Oma Falk - avatar
+ 3
That's partially possible, but how about help string, signature,... you can do it for each one .... or functools.wrap
22nd Apr 2022, 8:20 PM
Oma Falk
Oma Falk - avatar
+ 3
yes.. 😅 I didn't think about it. Thank you for the response.
22nd Apr 2022, 8:22 PM
Amir
Amir - avatar
+ 2
So what's the advantage of this? just keep the name? Why don't we do this => "wrapper_decorator.__name__ = func.__name__ " instead import functools?
22nd Apr 2022, 8:18 PM
Amir
Amir - avatar