I've always used functools.wraps
with my decorators, but today I've asked myself why?
def requires_auth(function):
@functools.wraps(function)
def wrapper(request, *args, **kwargs):
...
return function(request, *args, **kwargs)
return wrapper
This blog says:
Since decorator is simply a function that wraps original one so it will replace
function.__name__
property to it’s own name and that can cause some misunderstanding during debugging your code and(or) using introspection(inspect
/traceback
module and etc. in some certain conditions).
So functools.wraps
helps copying __name__
, __doc__
of the wrapped function, but my question is who needs it?
This answer says:
If using a decorator always meant losing this information about a function, it would be a serious problem.
But is this a "serious problem"?
This page says:
One final point thing to keep in mind is that trying to check the
__name__
and__doc__
of a wrapped function will get these values from the wrapper instead. This might mess things up for documentation generation tools or other kinds of code that rely on this information.
Ok, a valid point, but is this a critical problem?
I would rather not use functools.wraps
because I use pdb
a lot and if I get properties of a function with other function's properties it can make debugging harder.
Explicit is better than implicit.
All the information I found in favor of always using functools.wraps
does not look convincing. Have I missed anything?