The @ Syntax

def shout(fn):
    def wrapper(*args, **kwargs):
        return fn(*args, **kwargs).upper()
    return wrapper

@shout
def greet(name):
    return f"hello, {name}"

print(greet("ming"))   # HELLO, MING

Timing Decorator

import time
from functools import wraps

def timed(fn):
    @wraps(fn)
    def inner(*a, **kw):
        t = time.perf_counter()
        result = fn(*a, **kw)
        print(f"{fn.__name__} took {time.perf_counter()-t:.4f}s")
        return result
    return inner

Why functools.wraps?

Without it, the wrapped function loses its name and docstring. Always include it.

Common Built-in Decorators

Practise this on PyForm โ€” free

PyForm runs Python in your browser with an AI tutor trained for HKDSE. No install, no credit card.

Open PyForm โ†’