Question 1Easy
In Python, what is a decorator?
- A callable that takes a function and returns a replacement
- A special kind of class attribute shared by instances
- A keyword that makes a function private to its module
- A comment that documents what a function does
Answer: A. A callable that takes a function and returns a replacement
Explanation
@deco above def f is shorthand for f = deco(f). The decorator receives the function and returns what will be bound to the name.
Step-by-step solution
- @deco placed above def f is the same as f = deco(f).
- So a decorator is a callable that takes a function and returns a new object to use in its place.
- That matches option A, the correct answer.
Why the other options are wrong
- B. A class attribute shared by instances is not a decorator.
- C. Python has no keyword that makes a function private to its module.
- D. A comment is ignored by Python, but a decorator runs code.
Tip: @deco is shorthand for f = deco(f).
Question 2Medium
What is the output?
def deco(f):
def wrapper(*args):
return f(*args)
return wrapper
@deco
def greet():
pass
print(greet.__name__)
- wrapper
- greet
- f
- deco
Answer: A. wrapper
Explanation
Without functools.wraps, the decorated name refers to wrapper and its metadata. Adding @functools.wraps(f) to wrapper copies __name__, __doc__ and other metadata.
Step-by-step solution
- The decorator returns wrapper, so the name greet now refers to wrapper.
- Wrapper keeps its own name, which is 'wrapper', because nothing copied the original name.
- So print shows wrapper, the correct answer.
Why the other options are wrong
- B. Greet is the original name, but the decorated function now reports the wrapper's name.
- C. F is only a local name inside deco, not the name of the function.
- D. Deco is the decorator's name, not the name of the function.
Tip: Use @functools.wraps(f) on wrapper to keep the original name and docstring.
Question 4Medium
What is the output?
def a(f): return lambda: "a(" + f() + ")"
def b(f): return lambda: "b(" + f() + ")"
@a
@b
def g():
return "g"
print(g())
- a(b(g))
- b(a(g))
- a(g)
- b(g)
Answer: A. a(b(g))
Explanation
Stacked decorators apply bottom-up: g = a(b(g)). Calling the result runs a's wrapper outermost.
Step-by-step solution
- Decorators apply from the bottom up: first b wraps g, then a wraps that result.
- So g = a(b(g)).
- Calling g() runs a's wrapper first, so the output is a(b(g)), the correct answer.
Why the other options are wrong
- B. b(a(g)) is wrong; b is applied first, so b sits inside a.
- C. a(g) is wrong; b is applied too, so it appears in the result.
- D. b(g) is wrong; a is applied as well.
Tip: Stacked decorators apply from the bottom up, and the top one's wrapper runs first when the function is called.
Question 5Hard
What is the output?
def register(f):
print("reg", f.__name__, end="; ")
return f
@register
def task():
print("task")
print("start")
- reg task; task; start
- start; reg task
- reg task; start
- start
Answer: C. reg task; start
Explanation
A decorator runs once, when the def statement is executed, not when the function is called. task() is never called here.
Step-by-step solution
- The decorator register runs when the def statement runs, before print('start').
- It prints 'reg task; ' and returns the function unchanged.
- Task is never called, so the output is 'reg task; start', the correct answer.
Why the other options are wrong
- A. Task is printed only when task() is called, and it is not called here.
- B. Start is printed after reg task, because the def runs first.
- D. Reg task is printed first, by the decorator when the def runs.
Tip: A decorator runs once, when the def statement runs, not when the function is called.