Decorator Basics
A decorator is a function that wraps another function to add behavior before or after it runs. Apply with @decorator_name syntax.
A decorator is a function that wraps another function to add behavior before or after it runs. Apply with @decorator_name syntax.
def shout(func):
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
return result.upper()
return wrapper
@shout
def greet(name):
return f"Hello, {name}"
print(greet("world")) # HELLO, WORLD
@shout is shorthand for: greet = shout(greet)