nextRound
TechnologiesCoding ProblemsBookmarksLearning PathsLogin
nextRound
TechnologiesCoding ProblemsBookmarksLearning PathsLogin
Questions
2 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
02 / 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 the difference between a class-based decorator and a function-based decorator?

Difficulty: 8/10
Decorators, Class-Based Decorators, Descriptor Protocol

Class-based decorators use call and hold state; function-based use closures

A function-based decorator is a function that returns a closure over the wrapped function. A class-based decorator is a class whose instances are callable: init receives the function (or the parameters, in factory form), and call implements the behavior. The class form is preferable when the decorator has meaningful state or behavior of its own, for example a cache with a cache_clear() method, multiple helper methods, or inheritance. The function form is simpler and usually the right default. The subtle trap is that a class-based decorator instance is not a function, so it is not a descriptor by default and will not bind self when applied to an instance method unless you implement get.

  1. 1

    Use function-based for stateless or simple stateful wrapping; use class-based when you need extra public methods, inheritance, or richer state.

  2. 2

    Always call functools.update_wrapper(self, fn) in init for class-based decorators; functools.wraps is the function equivalent.

  3. 3

    Implement get on the class-based decorator to return a bound method when accessed through an instance, otherwise method binding breaks.

  4. 4

    Trade-off: class instances carry a per-decorated-function object and add an attribute lookup per call; closures are lighter but harder to extend.

  5. 5

    Common mistake: expecting a class-level counter to be shared across all decorated functions when the class is applied directly. Each application creates a new instance, so state is per-function.

  6. 6

    Version note: functools.update_wrapper and wrapped behavior is consistent across Python 3; the descriptor protocol used by get has been stable since Python 2.

javascript

Scenario Questions

0-2 years experience

  1. 1Which special method makes a class instance callable?
  2. 2What two things does a class-based decorator typically need to store on the instance?

2-5 years experience

  1. 1A class-based decorator works on a plain function but fails on an instance method. What is missing?
  2. 2You want one call counter shared across every decorated function. Which style do you choose and where does the state live?

5-8 years experience

  1. 1You need a decorated function to expose an extra method like cache_clear(). Which decorator style fits and why?
  2. 2How do you add descriptor support to a class-based decorator so that method binding and attribute access both work correctly?

8+ years experience

  1. 1Design a decorator library that lets users choose a function-based or class-based implementation behind one consistent public interface.
  2. 2Compare class-based and function-based decorators on per-call overhead, memory, introspection, and testability in CPython, and justify a default for a large codebase.

Follow-up Questions

  • What breaks if a class-based decorator is applied to a method without implementing __get__?
  • How would you share state across every function decorated by the same decorator class?
Sharethis question

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