[PYTHON] Decorator

decorator.py


def print_info(func):
    def wrapper(*args, **kwargs):
        print('start')
        result = func(*args,**kwargs)
        print('end')
        return result
    return wrapper

#Useful when adding something to a function (be careful in order when applying multiple decorators)
@print_info
def add_num(a, b):
    return a+b

r = add_num(10,20)
print(r)
#If you don't use a decorator, you can write as below, but it's hard to understand
#f = print_info(add_num)
#r = f(10,20)
#print(r)

output:

start
end
30

Recommended Posts

Decorator 1
Decorator 2
Decorator
Decorator
MyHDL decorator
Note: Python Decorator
Decorator to retry
Design Pattern #Decorator
Python function decorator
Write decorator in class
python decorator to retry
Make a function decorator
Summarize Python's @property decorator
Decorator pattern in Java
python decorator usage notes
Use a Property Decorator?
Python decorator operation memo
I tried Python> decorator