I have some functions in a class that are used for calculations. These functions all take a single argument, period, which is required to be int.
I want to use a decorator so that when I enter List[int] for that argument, it can be interpreted as taking that value as an argument and executing the function for all values in that list.
Since this decorator may be used on other functions with keyword arguments, I have to include *kwargs in the decorator input. I used the following reference code:
from typing import List
def multiple_para(func):
def wrapper(_, period, *args, **kw): # Since the first argument must be self, use _ instead
results = []
if isinstance(period, List):
for i in period:
result.append(func(_, period=i, *args, **kw))
else:
result = func(_, period=period, *args, **kw)
return result
return wrapper
class Test():
@multiple_para
def test(self, period: int, target: str = ''):
output = period + 1 # substitute calculation process
return output
if __name__ == '__main__':
test1 = Test()
test1.test(period=[1, 2, 3])
However, I get the following error message:
TypeError: test() got multiple values for argument 'period'