Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 06

Python Decorators — Wrap a Function with an Inner Function

Learn Python decorators as gift wrapping: square(getval) returns calculate(), pass the function not getval(), and reuse one wrapper for logging.

Intermediate35 min

![decorators gift wrapping function hero banner](data:image/svg+xml;charset=utf-8,%3Csvg%20font-family%3D%27ui-sans-serif%2C-apple-system%2Csystem-ui%2CArial%27%20width%3D%27100%25%27%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%20640%20220%27%3E%20%3Cdefs%3E%20%3ClinearGradient%20id%3D%27igddec6h%27%20x1%3D%270%27%20y1%3D%270%27%20x2%3D%271%27%20y2%3D%271%27%3E%20%3Cstop%20offset%3D%270%25%27%20stop-color%3D%27%237C3AED%27%2F%3E%20%3Cstop%20offset%3D%2755%25%27%20stop-color%3D%27%232563EB%27%2F%3E%20%3Cstop%20offset%3D%27100%25%27%20stop-color%3D%27%230F172A%27%2F%3E%20%3C%2FlinearGradient%3E%20%3C%2Fdefs%3E%20%3Crect%20width%3D%27640%27%20height%3D%27220%27%20rx%3D%2720%27%20fill%3D%27url%28%23igddec6h%29%27%2F%3E%20%3Crect%20x%3D%2736%27%20y%3D%2736%27%20width%3D%27360%27%20height%3D%27148%27%20rx%3D%2714%27%20fill%3D%27%230F172A%27%20opacity%3D%270.45%27%2F%3E%20%3Ctext%20x%3D%2752%27%20y%3D%2768%27%20fill%3D%27%23C4B5FD%27%20font-size%3D%2712%27%20font-weight%3D%27bold%27%3ELESSON%2006%20%C2%B7%20DECORATORS%3C%2Ftext%3E%20%3Ctext%20x%3D%2752%27%20y%3D%2798%27%20fill%3D%27%23FFFFFF%27%20font-size%3D%2720%27%20font-weight%3D%27bold%27%3EGift%20Wrapping%20a%20Function%3C%2Ftext%3E%20%3Ctext%20x%3D%2752%27%20y%3D%27122%27%20fill%3D%27%23E2E8F0%27%20font-size%3D%2712%27%3EWrap%20behavior%20without%20changing%20what%20is%20inside%3C%2Ftext%3E%20%3Ctext%20x%3D%2752%27%20y%3D%27148%27%20fill%3D%27%23A5B4FC%27%20font-size%3D%2711%27%3Einner%20function%20%C2%B7%20pass%20callable%20%C2%B7%20%40%20syntax%3C%2Ftext%3E%20%3Crect%20x%3D%27420%27%20y%3D%2748%27%20width%3D%27184%27%20height%3D%27124%27%20rx%3D%2712%27%20fill%3D%27%23FFFFFF%27%20opacity%3D%270.12%27%2F%3E%20%3Ctext%20x%3D%27440%27%20y%3D%2778%27%20fill%3D%27%23FDE68A%27%20font-size%3D%2710%27%20font-family%3D%27Courier%20New%27%3Esquare%28getval%29%3C%2Ftext%3E%20%3Ctext%20x%3D%27440%27%20y%3D%2798%27%20fill%3D%27%23BBF7D0%27%20font-size%3D%2710%27%20font-family%3D%27Courier%20New%27%3Ecalculate%28%29%3C%2Ftext%3E%20%3Ctext%20x%3D%27440%27%20y%3D%27118%27%20fill%3D%27%23E0F2FE%27%20font-size%3D%2710%27%20font-family%3D%27Courier%20New%27%3Empr%28%29%3C%2Ftext%3E%20%3Ctext%20x%3D%27440%27%20y%3D%27138%27%20fill%3D%27%23FBCFE8%27%20font-size%3D%2710%27%3Enot%20getval%28%29%3C%2Ftext%3E%20%3Ctext%20x%3D%27440%27%20y%3D%27158%27%20fill%3D%27%23FECACA%27%20font-size%3D%2710%27%3E%40logger%20preview%3C%2Ftext%3E%20%3C%2Fsvg%3E

Decorators = Gift Wrapping a Function

Think of decorators as wrapping an existing function with extra behavior — just like wrapping a gift without changing what is inside.

Real-life analogy: the original function stays the same

A decorator does not modify the gift — it only adds wrapping, ribbons, or a greeting card around it.

Imagine you are giving a gift

Your friend buys a phone. Before giving it, you wrap it beautifully. The phone never changes — only the presentation changes.

![phone getval wrapped by square decorator into gift calculate](data:image/svg+xml;charset=utf-8,%3Csvg%20font-family%3D%27ui-sans-serif%2C-apple-system%2Csystem-ui%2CArial%27%20width%3D%27100%25%27%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%20320%20120%27%3E%20%3Crect%20width%3D%27320%27%20height%3D%27120%27%20rx%3D%2714%27%20fill%3D%27%23FFFBEB%27%2F%3E%20%3Crect%20x%3D%2718%27%20y%3D%2736%27%20width%3D%2760%27%20height%3D%2748%27%20rx%3D%2710%27%20fill%3D%27%23E5E7EB%27%20stroke%3D%27%236B7280%27%2F%3E%20%3Ctext%20x%3D%2748%27%20y%3D%2752%27%20font-size%3D%278%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23374151%27%3EPhone%3C%2Ftext%3E%20%3Ctext%20x%3D%2748%27%20y%3D%2768%27%20font-size%3D%278%27%20text-anchor%3D%27middle%27%20fill%3D%27%236B7280%27%3Egetval%3C%2Ftext%3E%20%3Cpath%20d%3D%27M78%2060%20H108%27%20stroke%3D%27%2392400E%27%20stroke-dasharray%3D%273%203%27%2F%3E%20%3Cpolygon%20points%3D%27104%2C56%20112%2C60%20104%2C64%27%20fill%3D%27%2392400E%27%2F%3E%20%3Crect%20x%3D%27112%27%20y%3D%2728%27%20width%3D%2796%27%20height%3D%2764%27%20rx%3D%2712%27%20fill%3D%27%23F59E0B%27%2F%3E%20%3Cpath%20d%3D%27M160%2028%20V92%27%20stroke%3D%27%23FFFFFF%27%20stroke-width%3D%274%27%2F%3E%20%3Cpath%20d%3D%27M112%2060%20H208%27%20stroke%3D%27%23FFFFFF%27%20stroke-width%3D%274%27%2F%3E%20%3Ccircle%20cx%3D%27160%27%20cy%3D%2760%27%20r%3D%278%27%20fill%3D%27%23FFFFFF%27%2F%3E%20%3Ctext%20x%3D%27160%27%20y%3D%27108%27%20font-size%3D%278%27%20text-anchor%3D%27middle%27%20fill%3D%27%2392400E%27%3EDecorator%20square%3C%2Ftext%3E%20%3Cpath%20d%3D%27M208%2060%20H238%27%20stroke%3D%27%2392400E%27%20stroke-dasharray%3D%273%203%27%2F%3E%20%3Cpolygon%20points%3D%27234%2C56%20242%2C60%20234%2C64%27%20fill%3D%27%2392400E%27%2F%3E%20%3Crect%20x%3D%27242%27%20y%3D%2736%27%20width%3D%2760%27%20height%3D%2748%27%20rx%3D%2710%27%20fill%3D%27%2378350F%27%2F%3E%20%3Ctext%20x%3D%27272%27%20y%3D%2752%27%20font-size%3D%278%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23FDE68A%27%3EGift%3C%2Ftext%3E%20%3Ctext%20x%3D%27272%27%20y%3D%2768%27%20font-size%3D%278%27%20text-anchor%3D%27middle%27%20fill%3D%27%23FDE68A%27%3Ecalculate%3C%2Ftext%3E%20%3C%2Fsvg%3E

Python translation:

  • Phone = getval()
  • Gift wrapper = square()
  • Wrapped gift = calculate()

What you will learn

  • A decorator takes a normal function as parameter
  • It defines an inner function that adds behavior, then returns that inner function
  • mpr = square(getval) then mpr()
  • square(getval)() in one expression
  • Three separate helpers that each call getval() — no wrapping
  • Playground uses return 5.0, not input()

How a decorator works

![getval passes into square decorator returns calculate inner function](data:image/svg+xml;charset=utf-8,%3Csvg%20font-family%3D%27ui-sans-serif%2C-apple-system%2Csystem-ui%2CArial%27%20width%3D%27100%25%27%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%20320%20170%27%3E%20%3Crect%20width%3D%27320%27%20height%3D%27170%27%20rx%3D%2714%27%20fill%3D%27%23F5F3FF%27%2F%3E%20%3Crect%20x%3D%2760%27%20y%3D%2712%27%20width%3D%27200%27%20height%3D%2734%27%20rx%3D%278%27%20fill%3D%27%23DBEAFE%27%2F%3E%20%3Ctext%20x%3D%27160%27%20y%3D%2727%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%231D4ED8%27%3Egetval%28%29%3C%2Ftext%3E%20%3Ctext%20x%3D%27160%27%20y%3D%2737%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%231E40AF%27%3Ereturns%205.0%3C%2Ftext%3E%20%3Crect%20x%3D%2740%27%20y%3D%2764%27%20width%3D%27240%27%20height%3D%2742%27%20rx%3D%278%27%20fill%3D%27%23DDD6FE%27%2F%3E%20%3Ctext%20x%3D%27160%27%20y%3D%2780%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%235B21B6%27%3Esquare%28m%29%3C%2Ftext%3E%20%3Ctext%20x%3D%27160%27%20y%3D%2792%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%236D28D9%27%3EDecorator%3C%2Ftext%3E%20%3Crect%20x%3D%2740%27%20y%3D%27124%27%20width%3D%27240%27%20height%3D%2734%27%20rx%3D%278%27%20fill%3D%27%23A7F3D0%27%2F%3E%20%3Ctext%20x%3D%27160%27%20y%3D%27138%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23047857%27%3Ecalculate%28%29%3C%2Ftext%3E%20%3Ctext%20x%3D%27160%27%20y%3D%27148%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%23065F46%27%3EInner%20function%3C%2Ftext%3E%20%3C%2Fsvg%3E

The decorator receives a function, creates another function inside it, and returns that inner function.

Decorator syntax:

text
def FunctionName1(normal_function):
    def FunctionName2():
        # extra behavior
        ...
    return FunctionName2
  • FunctionName1 is the decorator
  • FunctionName2 is the inner function

Step-by-step code

Step 1 — Original function

python
def getval():
    return 5.0

This is just a normal function.

Step 2 — Decorator

python
def square(m):
    def calculate():
        n = m()
        return n ** 2
    return calculate

Notice:

  • m stores the original function
  • calculate() adds new behavior
  • return calculate sends back the wrapped function

Step 3 — Wrap it

python
mpr = square(getval)

getval is passed without brackets because you are passing the function itself.

Full example with prints

python
def square(m):
    print("Decorator Called")
    def calculate():
        print("Inner Function calling")
        n = m()
        res = n ** 2
        return res
    return calculate

mpr = square(getval)  # decorator call — does not run calculate yet
print(type(mpr))      # function
res = mpr()           # inner runs, getval runs
print("Square=", res)

Run the full script in the playground below.

What happens internally?

Think of this like a factory.

![square getval factory flow calculate m n squared 25.0](data:image/svg+xml;charset=utf-8,%3Csvg%20font-family%3D%27ui-sans-serif%2C-apple-system%2Csystem-ui%2CArial%27%20width%3D%27100%25%27%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%20320%20180%27%3E%20%3Crect%20width%3D%27320%27%20height%3D%27180%27%20rx%3D%2714%27%20fill%3D%27%23EFF6FF%27%2F%3E%20%3Crect%20x%3D%2716%27%20y%3D%2720%27%20width%3D%2780%27%20height%3D%2734%27%20rx%3D%278%27%20fill%3D%27%23DBEAFE%27%2F%3E%20%3Ctext%20x%3D%2756%27%20y%3D%2740%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%231D4ED8%27%3Egetval%3C%2Ftext%3E%20%3Crect%20x%3D%27122%27%20y%3D%278%27%20width%3D%2792%27%20height%3D%2758%27%20rx%3D%2710%27%20fill%3D%27%23C4B5FD%27%2F%3E%20%3Ctext%20x%3D%27168%27%20y%3D%2724%27%20font-size%3D%278%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%234C1D95%27%3Esquare%28%29%3C%2Ftext%3E%20%3Ctext%20x%3D%27168%27%20y%3D%2736%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%235B21B6%27%3Ereturns%3C%2Ftext%3E%20%3Ctext%20x%3D%27168%27%20y%3D%2748%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%235B21B6%27%3Ecalculate%3C%2Ftext%3E%20%3Crect%20x%3D%2798%27%20y%3D%2788%27%20width%3D%27140%27%20height%3D%2734%27%20rx%3D%278%27%20fill%3D%27%23A7F3D0%27%2F%3E%20%3Ctext%20x%3D%27168%27%20y%3D%27108%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23047857%27%3Ecalculate%28%29%3C%2Ftext%3E%20%3Crect%20x%3D%27116%27%20y%3D%27142%27%20width%3D%27104%27%20height%3D%2726%27%20rx%3D%278%27%20fill%3D%27%232563EB%27%2F%3E%20%3Ctext%20x%3D%27168%27%20y%3D%27159%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23FFFFFF%27%3E25.0%3C%2Ftext%3E%20%3C%2Fsvg%3E

Line What happens
square(getval) Decorator runs
return calculate Gives back inner function
mpr() Inner function executes
m() Calls original getval()
n ** 2 Returns square 25.0

Two ways to call it

Method 1 — Store the wrapped function

python
mpr = square(getval)
result = mpr()
print(result)   # 25.0

Easier to reuse multiple times.

Method 2 — One line

python
result = square(getval)()
print(result)   # 25.0

Creates and immediately executes the wrapper.

Both produce the same answer: 25.0.

The most common mistake

Students often write this:

Wrong:

python
square(getval())

Here getval() runs immediately and returns 5.0. A float is not a function — the inner call m() would fail.

Correct:

python
square(getval)

Here you pass the function object itself. The decorator can call m() later.

Without a decorator

Three helpers each call getval() for square, square-root, and cube. That works, but the extra math lives in new functions, not as a wrapper around getval:

python
def square_print():
    n = getval()
    print("square({})={}".format(n, n ** 2))

def squareroot_print():
    n = getval()
    print("squareroot({})={}".format(n, n ** 0.5))

def cube_print():
    n = getval()
    print("cube({})={}".format(n, n ** 3))

Why do decorators exist?

Imagine 50 functions in your application. Every one needs logging.

Without decorators — repeated code:

python
def login():
    print("Logging...")
    ...

def payment():
    print("Logging...")
    ...

With a decorator — one reusable wrapper:

python
@logger
def login():
    ...

@logger
def payment():
    ...

![decorator use cases logging timing auth caching](data:image/svg+xml;charset=utf-8,%3Csvg%20font-family%3D%27ui-sans-serif%2C-apple-system%2Csystem-ui%2CArial%27%20width%3D%27100%25%27%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%20320%2090%27%3E%20%3Crect%20width%3D%27320%27%20height%3D%2790%27%20rx%3D%2712%27%20fill%3D%27%23F8FAFC%27%2F%3E%20%3Crect%20x%3D%2712%27%20y%3D%2716%27%20width%3D%2768%27%20height%3D%2758%27%20rx%3D%278%27%20fill%3D%27%23EDE9FE%27%20stroke%3D%27%237C3AED%27%2F%3E%20%3Ctext%20x%3D%2746%27%20y%3D%2738%27%20font-size%3D%278%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%235B21B6%27%3ELogging%3C%2Ftext%3E%20%3Ctext%20x%3D%2746%27%20y%3D%2752%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%236D28D9%27%3E%40logger%3C%2Ftext%3E%20%3Crect%20x%3D%2788%27%20y%3D%2716%27%20width%3D%2768%27%20height%3D%2758%27%20rx%3D%278%27%20fill%3D%27%23DBEAFE%27%20stroke%3D%27%232563EB%27%2F%3E%20%3Ctext%20x%3D%27122%27%20y%3D%2738%27%20font-size%3D%278%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%231D4ED8%27%3ETiming%3C%2Ftext%3E%20%3Ctext%20x%3D%27122%27%20y%3D%2752%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%231E40AF%27%3Estopwatch%3C%2Ftext%3E%20%3Crect%20x%3D%27164%27%20y%3D%2716%27%20width%3D%2768%27%20height%3D%2758%27%20rx%3D%278%27%20fill%3D%27%23D1FAE5%27%20stroke%3D%27%23059669%27%2F%3E%20%3Ctext%20x%3D%27198%27%20y%3D%2738%27%20font-size%3D%278%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23047857%27%3EAuth%3C%2Ftext%3E%20%3Ctext%20x%3D%27198%27%20y%3D%2752%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%23065F46%27%3Elogin%20check%3C%2Ftext%3E%20%3Crect%20x%3D%27240%27%20y%3D%2716%27%20width%3D%2768%27%20height%3D%2758%27%20rx%3D%278%27%20fill%3D%27%23FDE68A%27%20stroke%3D%27%23D97706%27%2F%3E%20%3Ctext%20x%3D%27274%27%20y%3D%2738%27%20font-size%3D%278%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%2392400E%27%3ECache%3C%2Ftext%3E%20%3Ctext%20x%3D%27274%27%20y%3D%2752%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%2378350F%27%3Ereuse%20result%3C%2Ftext%3E%20%3C%2Fsvg%3E

Common real-world uses: logging, execution timing, authentication, and caching.

Without vs with decorator

![without decorator repeated square cube root helpers](data:image/svg+xml;charset=utf-8,%3Csvg%20font-family%3D%27ui-sans-serif%2C-apple-system%2Csystem-ui%2CArial%27%20width%3D%27100%25%27%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%20160%20120%27%3E%20%3Crect%20width%3D%27160%27%20height%3D%27120%27%20rx%3D%2710%27%20fill%3D%27%23F9FAFB%27%2F%3E%20%3Ctext%20x%3D%2780%27%20y%3D%2718%27%20font-size%3D%278%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%2364748B%27%3EWithout%20decorator%3C%2Ftext%3E%20%3Crect%20x%3D%2714%27%20y%3D%2728%27%20width%3D%27132%27%20height%3D%2722%27%20rx%3D%276%27%20fill%3D%27%23E5E7EB%27%2F%3E%20%3Ctext%20x%3D%2780%27%20y%3D%2742%27%20font-size%3D%278%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23374151%27%3Esquare_print%28%29%3C%2Ftext%3E%20%3Crect%20x%3D%2714%27%20y%3D%2756%27%20width%3D%27132%27%20height%3D%2722%27%20rx%3D%276%27%20fill%3D%27%23E5E7EB%27%2F%3E%20%3Ctext%20x%3D%2780%27%20y%3D%2770%27%20font-size%3D%278%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23374151%27%3Esquareroot_print%28%29%3C%2Ftext%3E%20%3Crect%20x%3D%2714%27%20y%3D%2784%27%20width%3D%27132%27%20height%3D%2722%27%20rx%3D%276%27%20fill%3D%27%23E5E7EB%27%2F%3E%20%3Ctext%20x%3D%2780%27%20y%3D%2798%27%20font-size%3D%278%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23374151%27%3Ecube_print%28%29%3C%2Ftext%3E%20%3C%2Fsvg%3E

![with decorator one logger wraps many functions](data:image/svg+xml;charset=utf-8,%3Csvg%20font-family%3D%27ui-sans-serif%2C-apple-system%2Csystem-ui%2CArial%27%20width%3D%27100%25%27%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%20160%20120%27%3E%20%3Crect%20width%3D%27160%27%20height%3D%27120%27%20rx%3D%2710%27%20fill%3D%27%23F5F3FF%27%2F%3E%20%3Ctext%20x%3D%2780%27%20y%3D%2718%27%20font-size%3D%278%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%236D28D9%27%3EWith%20decorator%3C%2Ftext%3E%20%3Crect%20x%3D%2720%27%20y%3D%2728%27%20width%3D%27120%27%20height%3D%2722%27%20rx%3D%276%27%20fill%3D%27%23C4B5FD%27%2F%3E%20%3Ctext%20x%3D%2780%27%20y%3D%2742%27%20font-size%3D%278%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%234C1D95%27%3E%40logger%3C%2Ftext%3E%20%3Crect%20x%3D%2728%27%20y%3D%2758%27%20width%3D%27104%27%20height%3D%2716%27%20rx%3D%275%27%20fill%3D%27%23A7F3D0%27%2F%3E%20%3Ctext%20x%3D%2780%27%20y%3D%2769%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%23065F46%27%3Elogin%28%29%3C%2Ftext%3E%20%3Crect%20x%3D%2728%27%20y%3D%2778%27%20width%3D%27104%27%20height%3D%2716%27%20rx%3D%275%27%20fill%3D%27%23A7F3D0%27%2F%3E%20%3Ctext%20x%3D%2780%27%20y%3D%2789%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%23065F46%27%3Epayment%28%29%3C%2Ftext%3E%20%3Crect%20x%3D%2728%27%20y%3D%2798%27%20width%3D%27104%27%20height%3D%2716%27%20rx%3D%275%27%20fill%3D%27%23A7F3D0%27%2F%3E%20%3Ctext%20x%3D%2780%27%20y%3D%27109%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%23065F46%27%3Esearch%28%29%3C%2Ftext%3E%20%3C%2Fsvg%3E

The next lesson covers @ syntax and stacking decorators.

Interview cheat sheet

Question Answer
Decorator receives? A function
Decorator returns? Inner function
Purpose? Add behavior without changing original code
Common mistake? Passing getval() instead of getval
One-line call? square(getval)()

30-second memory map

![original function decorator inner function result recap flow](data:image/svg+xml;charset=utf-8,%3Csvg%20font-family%3D%27ui-sans-serif%2C-apple-system%2Csystem-ui%2CArial%27%20width%3D%27100%25%27%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%20320%20170%27%3E%20%3Crect%20width%3D%27320%27%20height%3D%27170%27%20rx%3D%2714%27%20fill%3D%27%23F8FAFC%27%2F%3E%20%3Crect%20x%3D%2760%27%20y%3D%2714%27%20width%3D%27200%27%20height%3D%2724%27%20rx%3D%276%27%20fill%3D%27%23DBEAFE%27%2F%3E%20%3Ctext%20x%3D%27160%27%20y%3D%2730%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%231D4ED8%27%3EOriginal%20Function%3C%2Ftext%3E%20%3Crect%20x%3D%2760%27%20y%3D%2750%27%20width%3D%27200%27%20height%3D%2724%27%20rx%3D%276%27%20fill%3D%27%23DDD6FE%27%2F%3E%20%3Ctext%20x%3D%27160%27%20y%3D%2766%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%235B21B6%27%3EDecorator%3C%2Ftext%3E%20%3Crect%20x%3D%2760%27%20y%3D%2786%27%20width%3D%27200%27%20height%3D%2724%27%20rx%3D%276%27%20fill%3D%27%23D1FAE5%27%2F%3E%20%3Ctext%20x%3D%27160%27%20y%3D%27102%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23047857%27%3EInner%20Function%3C%2Ftext%3E%20%3Crect%20x%3D%2760%27%20y%3D%27122%27%20width%3D%27200%27%20height%3D%2724%27%20rx%3D%276%27%20fill%3D%27%23FDE68A%27%2F%3E%20%3Ctext%20x%3D%27160%27%20y%3D%27138%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%2392400E%27%3EResult%3C%2Ftext%3E%20%3C%2Fsvg%3E

Remember this sentence:

A decorator wraps a function, creates an inner function, and returns it so extra behavior can be added without modifying the original function.

Next: @ syntax and stacked decorators.

Loading playground…

Check your understanding

Decorators overview quiz

A decorator is:

1 / 10