Advanced Python Programming — Decorator

Understand wrapping → implement decorators → compose a real service pipeline
TAB 1 · CONCEPT

See What a Decorator Actually Does

A decorator receives a function, wraps extra behavior around it, and returns a callable — without editing the original function body.

welcome()
→
@add_log
→
wrapper()
→
enhanced welcome()

Python Workspace

PYTHON CONSOLE Ready.
TAB 2 · IMPLEMENT

Build Two Decorator Patterns

A · Simple decorator

Create uppercase(func). Its wrapper receives text and returns the original result converted to uppercase.

Expected: HELLO PYTHON
B · Decorator with parameters

Create repeat(times) so @repeat(3) executes the decorated function three times.

Expected: PING:API printed 3 times

Debug rule: Python exceptions must appear directly in the Python Console. The Cell does not replace them with a custom UI error.

Your Decorators

PYTHON CONSOLE Ready.
TAB 3 · REAL CASE

Decorate a Checkout Service

Build a reusable service pipeline without putting logging, validation and fee logic inside checkout().

  1. @audit — print the user before execution.
  2. @require_positive — reject amount ≤ 0 with a real ValueError.
  3. @fee(0.05) — add a 5% service fee.
  4. Keep checkout() focused only on the core amount.
audit
→
validate
→
fee
→
checkout
Expectation
AUDIT user=KASA
1050.0

After success, try checkout("KASA", -100) and inspect Python's real traceback.

Service Pipeline

PYTHON CONSOLE Ready.