Lesson 07
Python @decorator Syntax — Stack square, sqrt, cube, and Text Pipelines
Learn @ decorator syntax visually: phone-plus-apps analogy, bottom-up stack order, stacked math tuple, and text pipeline.
 |
| Apps | Decorators |
| Enhanced phone | Wrapped function |
What you will learn
@squareabovedef getvalequalsgetval = square(getval)@squareunpacksnum, result- Stacked
@cube@squareroot@square— closest decorator runs first - Text pipeline: upper, lower, reverse words
- Playground literals:
5.0and"Python Filter Map"— noinput()
What @square actually means
These two are 100% identical:
:
return 5.0
getval = square(getval)With @:
@square
def getval():
return 5.0Easy memory rule:
@square= "After creating this function, wrap it withsquare()."
@square
def square(m):
def calculate():
n = m()
res = n ** 2
return n, res
return calculate
@square
def getval():
return 5.0
num, result = getval()
print("Square({})={}".format(num, result))Calling getval() actually runs calculate.
Execution order — the biggest interview question
Many students think decorators run top to bottom. Wrong. They execute from the bottom upward — the decorator closest to def runs first.
:
return 5.0Execution order:
@square(closest todef)@squareroot@cube(outermost)
Interactive math decorator stack
Change the number in the playground and watch every decorator add a new value. With 5.0:

# (5.0, 25.0, ~2.236, 125.0)Application order (bottom first):
square—(n, n**2)squareroot— addsn ** 0.5cube— addsn ** 3
n, sqv, sqrtv, cb = getval()
print("Square({})={}".format(n, sqv))
print("SquareRoot({})={}".format(n, sqrtv))
print("Cube({})={}".format(n, cb))For 5.0: square 25.0, square root ≈ 2.236, cube 125.0.
Data flows through each layer
Think of decorators like a factory conveyor belt. Every decorator keeps the previous result and appends something new.
:
return "Python Filter Map"Bottom first: original line, then upper, then lower, then each word reversed. The example below also prints rwd.upper() and rwd.lower() of the reversed string.
NL, UL, LL, rwd = getLine()
print("Given:", NL)
print("Upper:", UL)
print("Lower:", LL)
print("Reversed upper:", rwd.upper())
print("Reversed lower:", rwd.lower())Run the full script in the playground below.
Three common mistakes
- Reading top to bottom — decorators execute bottom first (closest
@todef). - Forgetting to call the wrapped function — inside the decorator, use
m(), not justm. - Thinking
@is magic syntax — it is simply shorthand forfunction = decorator(function).
Interview cheat sheet
| Question | Answer |
|---|---|
What does @f mean? |
name = f(name) |
| Stack order? | Bottom decorator first |
| Inner function must? | Call the wrapped callable (m(), m()) |
| Stacked math unpack? | n, square, sqrt, cube |
30-second memory map
.
You finished Iterators, Generators, and Decorators. Revisit Lambda and special functions for lazy filter/map, or Functions for def and scope.
Loading playground…
Check your understanding
Decorator stacking quiz
@square above def getval is equivalent to:
1 / 10