Python Decorators

Decorators allow modifying functions dynamically.

Snippet:

def decorator(func):\n    def wrapper():\n        print("Before function call")\n        func()\n        print("After function call")\n    return wrapper\n\n@decorator\ndef say_hello():\n    print("Hello!")\nsay_hello()

Example:

def uppercase(func):\n    def wrapper():\n        return func().upper()\n    return wrapper\n\n@uppercase\ndef greet():\n    return "hello"\nprint(greet())