-
Notifications
You must be signed in to change notification settings - Fork 72
Closed
Description
I have a use case where I pass to a function function func1 another function func2 as input, and then inside the function func1, new arguments are prepared and calling func2 with these new arguments... as this function func2 could be one of many it is not very practical to add decorator for each option but instead I would like to apply wrapper inside func1...
A demonstration is:
from functools import wraps, partial
def my_decorator(f):
@wraps(f)
def wrapper(*args, **kwds):
print('Calling decorated function')
return f(*args, **kwds)
return wrapper
# @my_decorator
def example(a, b=2):
"""Docstring"""
print('Called example function with arguments:', a, b)
my_decorator(example)(1)
# example.__name__
# example.__doc__
example_ = partial(example, b=4)
my_decorator(example_)(1)It is related to my work on Lightning-AI/torchmetrics#2335 where some functions can have implementation inside test, some would be coming straight from sklearn and some would be with functools.partial