I have an addition function with two parameters a and b which simply adds a and b. To round this number, I have made a decorator factory which takes a decimals parameter and round the results of the addition function to the specified number of decimals. However, this requires the decimals number to be set at function definition. I am looking for a more dynamic approach.
Is it instead possible to make the decorator alter the addition function, such that the addition function gets a new parameter "decimals"? Here is my code:
import functools
def rounding(decimals):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
return round(func(*args, **kwargs), decimals)
return wrapper
return decorator
@rounding(decimals=2)
def addition(a: float, b: float):
return a + b
addition(a=1.0003, b=2.01)
So this makes the addition function always round to 2 decimals. What I instead want my decorator to do, is add a new arguments, such that I can call
addition(a=1.0003, b=2.01, decimals=2)
Is there a way to do this and if yes, is there a way to do this such that function docs still shows a, b and decimals instead of *args, **kwargs (for example when pressing ctrl+P in pycharm on the function)
I have just started working my way around python decorators. I have used https://realpython.com/primer-on-python-decorators/ as inspiration. I have not been able to find an existing answer on this site. The nearest I got was related to partial functions like here, but perhaps I am just searching for the wrong thing.
I am using Python 3.10.