I have a decorator factory which just multiplies some given numbers.
from functools import wraps
def multiply(numbers):
def decorator(f):
def wrapper(*args, **kwargs):
num1 = numbers[0]
for num2 in numbers[1:]:
num1 = num1 * num2
print(num1)
return f(*args)
return wrapper
return decorator
@multiply(numbers=[1, 2, 3])
def input(**kwargs):
print("That was the multiplied number.")
input()
(I know this decorator is useless but this is for just for learning)
Now, if I wanted to use @wraps
, where would I put it?
for using @wraps you just can not directly get started with placing it on a single place. You will need to import it and thn start using it. @wraps can be imported from functools. Thn you can directly use it in the inner wrapper function inside your decorator.
You will import like :
from functools import wraps
Thn use it like below :
def multiply(numbers):
def decorator(f):
@wraps(f) #You can use it like this here after decorator.
def wrapper(*args, **kwargs):
num1 = numbers[0]
for num2 in numbers[1:]:
num1 = num1 * num2
print(num1)
return f(*args)
return wrapper
return decorator
@multiply(numbers=[1, 2, 3])
def input(**kwargs):
print("That was the multiplied number.")
input()
You can ask more if that is not clear.