1
Lecture 5 Advanced Decorators
Tirsvad edited this page 2026-11-06 06:08:37 +01:00

English · Dansk · Bahasa Melayu

Lecture 5: Advanced decorators

Angela is logged in and gets a post, Bob is not and gets none

Code: src/higher_lower/presentation/lectures/step_05_authenticated.py.

What we gained

  • *args collects positional arguments and **kwargs collects keyword arguments, so a decorator works for any function.
  • is_authenticated reads the user from the arguments and runs the function only when user.is_logged_in is true.
  • A decorator can control whether a function runs at all, not only change its result.
  • The same pattern protects any function that needs a logged-in user.