Python Simple Decorator
@decorator Practice

Wrap a function without editing its body

A decorator receives a function and returns a new function that adds start/end logging.

Picture this: a gift wrap around a function

The decorator runs extra code before/after the original function.

@wrapperwrap
→
def f()original
→
call f()hits wrapper first
→
then f bodythe gift

In easy words

@log_wrap above def hello() means hello = log_wrap(hello). Calls go through the wrapper first, then the original body.

Where this shows up

Auth checks, timing, retries, logging — any cross-cutting concern you want reusable.

Key facts

  • Syntax — @wrap above a def
  • Mechanism — wrap returns an inner function
  • *args **kwargs — Forward any call signature
  • Return — Pass through the original return value

What to remember

  • Order — start → original → end
  • Practice — print start/end around hello
  • Do not mutate — Wrapper adds behavior only
  • Stacks — Multiple @decorators apply bottom-up
Call timeline
Ready

Example Program

1def log_wrap(fn):
2def inner(*args, **kwargs):
3print("start")
4out = fn(*args, **kwargs)
5print("end")
6return out
7return inner