Learning decorators and I wanted to pass a function with arguments to a decorator function like shown below but I always get a syntax error. Please help!
def decorator(func(*args)):
    def wrapper():
        print('up')
        func(*args)
        print('down')
    return wrapper
@decorator
def func(n):
    print(n)
func('middle')
Output:
File "<ipython-input-21-c2d727543f8e>", line 1
    def decorator(func(*args)):
                      ^
SyntaxError: invalid syntax
Advertisement
Answer
Your syntax is incorrect. To define a correct decorator and wrapper function, see what follows:
def decorator(func):       # assign the function you want to pass
    def wrapper(*args):    # assign the parameters you want to use for the function
        print('up')
        func(*args)
        print('down')
    return wrapper
@decorator
def func(n):               # The function you want to decorate
    print(n)
func('middle')
Output:
up middle down
