nextRound
TechnologiesCoding ProblemsBookmarksLearning PathsLogin
nextRound
TechnologiesCoding ProblemsBookmarksLearning PathsLogin
Questions
5 of 8
1Why is it important to use functools.wraps when writing a decorator?
2What is the difference between a class-based decorator and a function-based decorator?
3How does the yield keyword turn a function into a generator, and what happens to the function's state between calls?
4What is the difference between yield and yield from?
5What is a decorator, and how does it use closures to wrap a function's behavior?
6What is the difference between an iterable and an iterator? What protocol must an object implement to be an iterator?
7How would you write a decorator that accepts arguments (e.g., @retry(times=3))?
8How would you process a very large file that doesn't fit in memory using generators?
PythonPython
Basics
Control Flow and Functions
Data Structures
Comprehensions & Functional Programming
Iterators, Generators & Decorators
Object-Oriented Programming
Exception Handling & Debugging
Concurrency & Parallelism
Performance & Optimization
Testing
Security
Modules, Packaging & Environment
Type Hinting & Modern Python
System Design & Architecture with Python
Best Practices & Design Patterns
Edge Cases & Tricky Interview Questions
05 / 08
nextRound

AI-powered interview preparation platform. Practice with curated questions, mock interviews, and personalized learning paths to crack your dream tech interview.

Quick Links

  • Technologies
  • Mock Interviews
  • Saved Questions
  • Pricing

Company

  • About Us
  • Contact Us

Legal

  • Privacy Policy
  • Terms of Use

© 2026 nextRound. All rights reserved.

What is a decorator, and how does it use closures to wrap a function's behavior?

Difficulty: 5/10
Decorators, Closures, Higher-Order Functions

A decorator is a callable that takes a function and returns a replacement, using a closure to capture it

A decorator is a higher-order function: it accepts a callable and returns a callable that is bound to the original name. The @decorator syntax is pure sugar applied at definition time, so @logged above def add(...) is exactly add = logged(add). The wrapper function works because of a closure: it references the parameter fn from the enclosing scope, and Python stores that reference in a cell on the wrapper's closure so the original function stays alive after logged returns. This is what lets the wrapper run code before and after the wrapped call without the caller knowing.

  1. 1

    Order matters: decorators apply bottom-up. @a above @b means a(b(f)), so the topmost decorator is the outermost wrapper.

  2. 2

    Use functools.wraps inside the wrapper so the replacement keeps the original metadata, otherwise you break docs, debugging, and introspection-based frameworks.

  3. 3

    Trade-off: decorators add a call frame per invocation and can hide control flow. Explicit helper functions or context managers are sometimes clearer and easier to test.

  4. 4

    Common mistake: forgetting to return the wrapper, which silently rebinds the name to None and produces TypeError: 'NoneType' object is not callable.

  5. 5

    Common mistake: forgetting *args, **kwargs, which makes the decorated function reject any arguments it actually accepts.

  6. 6

    Version note: PEP 318 introduced decorator syntax in Python 2.4, class decorators came in 2.6, and the wrapped attribute was added in 3.2.

javascript

Scenario Questions

0-2 years experience

  1. 1What does @my_decorator actually do to the function name at import time?
  2. 2Why does a decorator that forgets to return the wrapper make the function unusable?

2-5 years experience

  1. 1Two decorators are stacked on one function. In what order do they execute and how do you reason about it?
  2. 2A decorator works on a plain function but fails on a method with self. What is the cause?

5-8 years experience

  1. 1You need one decorator that handles both sync and async functions. How do you detect the type and dispatch?
  2. 2You need behavior that can be toggled per call rather than at definition time. What alternatives to decorators would you consider?

8+ years experience

  1. 1Design a decorator framework supporting caching, retries, and metrics without surprising stacking-order interactions.
  2. 2How do decorators interact with typing, inspect.signature, and static analysis, and how do you keep a decorator library type-safe for users?

Follow-up Questions

  • How do you write a decorator that works on both a plain function and an instance method?
  • What is the difference between a decorator and a context manager, and when would you pick each?
Sharethis question

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.