Decorators zijn een krachtige Python feature die je code DRY en leesbaar houdt. In deze learning ga ik van basis tot gevorderd gebruik.
Basis decorator
def my_decorator(func):\n def wrapper(*args, **kwargs):\n print('Before')\n result = func(*args, **kwargs)\n print('After')\n return result\n return wrapper\n\n@my_decorator\ndef say_hello():\n print('Hello\!')
Praktisch gebruik
Decorators worden veel gebruikt voor: logging, timing, authentication, caching en retry logic.