Decorators Basics
A decorator wraps a function to add behaviour. Use functools.wraps to keep the name and docstring.
Example
from functools import wraps
def shout(f):
@wraps(f)
def w(*a, **k):
return f(*a, **k).upper()
return w
@shout
def hi(n):
return "hi " + n
print(hi("ada"), hi.__name__)Result
HI ADA hi
Exercise
Apply a decorator.
shoutQuiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.