capypad
0 day streak
python / intermediate
Snippet

Function Decorators

Decorators allow you to wrap another function to extend its behavior without permanently modifying it. They are often used for logging, access control, or caching.

snippet.py
python
1
2
3
4
5
6
7
8
9
def log_call(func):
def wrapper(*args, **kwargs):
print(f"Calling {func.__name__}")
return func(*args, **kwargs)
return wrapper
 
@log_call
def greet(name):
return f"Hello, {name}"
Breakdown
1
def log_call(func):
Defines a decorator function that accepts another function as an argument.
2
@log_call
Applies the decorator to the greet function, effectively wrapping it.