Answer key · 12 questions

Python Decorators Exercises With Answers

All 12 questions for Python Decorators in one place. Try the practice page first, then use this page to check the exact answer and the reason.

Quick answer

This answer key contains 4 multiple-choice, 4 fill-in-the-blank, and 4 output-prediction questions for Python Decorators, with the exact answer and a short explanation for every question.

The answers are shown in full, so use this page after making an attempt. The practice page is the better first pass.

01 · Multiple choice

What does a decorator receive and return?

  1. A class and an instance
  2. A string and a number
  3. A function and a replacement callablecorrect
  4. A module and a package

Answer: A function and a replacement callable

Why: A decorator takes a function and returns a callable that usually wraps it.

02 · Multiple choice

What does the program output?

def deco(f):
    def wrapper():
        return f() + "!"
    return wrapper

@deco
def hi():
    return "hi"
print(hi())
  1. hi!correct
  2. None
  3. hi
  4. !hi

Answer: hi!

Why: The wrapper adds an exclamation mark to the original result.

03 · Multiple choice

What is @deco above def f equivalent to?

  1. f = f(deco)
  2. deco = f(deco)
  3. f = deco(f)correct
  4. deco(f) without assigning

Answer: f = deco(f)

Why: The decorator is applied at definition time and its result replaces the name f.

04 · Multiple choice

Why use functools.wraps?

  1. To preserve the original function metadatacorrect
  2. To make the wrapper run faster
  3. To call the function twice
  4. To convert the function to a class

Answer: To preserve the original function metadata

Why: wraps copies the name, docstring, and other metadata to the wrapper.

05 · Fill in the blank

Complete the module that provides wraps.

from ____ import wraps

Answer: functools

Why: wraps lives in the functools module.

06 · Fill in the blank

Complete the decorator return value.

def deco(f):
    def wrapper():
        return f()
    return ____

Answer: wrapper

Why: The decorator must return the inner wrapper function.

07 · Fill in the blank

Complete the parameter that collects positional arguments.

def deco(f):
    def wrapper(*args, **kwargs):
        return f(*____, **kwargs)
    return wrapper

Answer: args

Why: The wrapper forwards all positional arguments through args.

08 · Fill in the blank

Complete the metadata helper.

from functools import ____

Answer: wraps

Why: wraps keeps the decorated function's identity.

09 · Predict output

What appears on the screen?

def deco(f):
    def wrapper():
        return f() * 2
    return wrapper

@deco
def word():
    return "ab"
print(word())

Answer: abab

Why: The wrapper doubles the string, so ab becomes abab.

10 · Predict output

What is printed?

def deco(f):
    def wrapper():
        return f().upper()
    return wrapper

@deco
def word():
    return "hi"
print(word())

Answer: HI

Why: The wrapper uppercases the result, so hi becomes HI.

11 · Predict output

What is the output?

def deco(f):
    def wrapper():
        return "[" + f() + "]"
    return wrapper

@deco
def word():
    return "ok"
print(word())

Answer: [ok]

Why: The wrapper adds square brackets around ok.

12 · Predict output

What does this code print?

from functools import wraps
def deco(f):
    @wraps(f)
    def wrapper():
        return f()
    return wrapper
@deco
def hello():
    return "hi"
print(hello.__name__)

Answer: hello

Why: wraps copies the original name, so the output is hello.

Try the questions first

Open the practice page for Python Decorators and answer the same 12 questions with instant feedback.

Practice this lesson