nextRound
TechnologiesCoding ProblemsBookmarksLearning PathsLogin
nextRound
TechnologiesCoding ProblemsBookmarksLearning PathsLogin
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.

Questions
2 of 19
1How would you make a custom class usable in a for loop?
2How do __enter__ and __exit__ work together to implement the context manager protocol?
3How does Python implement inheritance, and what is the Method Resolution Order (MRO)?
4What are dataclasses, and how do they reduce boilerplate compared to manually writing __init__, __eq__, and __repr__?
5What is the @property decorator, and what problem does it solve compared to plain getter/setter methods?
6What is the difference between method overriding and method overloading, and why doesn't Python natively support overloading?
7What problem does the diamond inheritance pattern cause, and how does Python's MRO resolve it?
8What is the difference between a class and an instance in Python?
9How does Python implement encapsulation given that it has no true 'private' access modifiers?
10What is the purpose of self in instance methods, and why must it be explicitly declared in Python?
11What is the difference between __str__ and __repr__, and what convention should __repr__ follow?
12What is a metaclass, and what is type's relationship to every class in Python?
13What is the purpose of __eq__ and __hash__, and why must they be implemented consistently?
14What is a descriptor, and how do @property and ORMs like Django's models rely on the descriptor protocol?
15What is the difference between an instance method, a class method, and a static method?
16What is composition, and why is 'favor composition over inheritance' often recommended?
17What are dunder (magic) methods, and how do they enable operator overloading?
18When would you use a metaclass instead of a class decorator or __init_subclass__?
19How do abstract base classes (abc.ABC) enforce interface contracts in Python?
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 / 19

How do __enter__ and __exit__ work together to implement the context manager protocol?

Difficulty: 8/10
Context Managers, With Statement, Dunder Methods

with calls enter and guarantees exit for cleanup, including on exceptions

The with statement calls type(cm).enter(cm) and binds the return value to the as target, then always calls exit on the way out, whether the block completed, raised, or was exited early. exit receives the exception type, value, and traceback. Returning True suppresses the exception; returning anything falsy lets it propagate. That is the whole protocol, and it is why with is strictly better than try/finally for resource cleanup: it is shorter, harder to misuse, and composes. The contextlib module provides contextmanager for generator-based context managers and ExitStack for combining many of them dynamically.

  1. 1

    enter can return self, a resource, or None. Choose deliberately so the as target is useful.

  2. 2

    exit must not swallow exceptions by accident. Returning True suppresses, returning None propagates.

  3. 3

    contextlib.contextmanager turns a generator with one yield into a context manager; the code after yield runs as exit.

  4. 4

    contextlib.ExitStack is the right tool when the number of resources is dynamic or conditionally acquired.

  5. 5

    Trade-off: exit cannot easily distinguish a normal exit from an exception unless you inspect the arguments. Do not over-engineer that distinction.

  6. 6

    Common mistake: doing work only in enter without a matching exit path, which leaks resources on exceptions.

  7. 7

    Version note: the with statement landed in Python 2.5, contextlib in 2.5, and ExitStack in 3.3.

javascript

Scenario Questions

0-2 years experience

  1. 1Which two methods must a context manager implement?
  2. 2What does the with statement do if the body raises?

2-5 years experience

  1. 1You write a context manager that swallows every exception. Why is that dangerous?
  2. 2You need to manage a variable number of files in a loop. Which contextlib tool fits?

5-8 years experience

  1. 1You need a context manager that only rolls back the transaction on specific exception types. How do you implement it?
  2. 2You are managing resources in an async function. What changes compared to a sync context manager?

8+ years experience

  1. 1Design a context manager library for nested resource acquisition with deterministic cleanup and observability on failure.
  2. 2Explain how async context managers, ExitStack, and asynccontextmanager compose, and where the guarantees differ from sync versions.

Follow-up Questions

  • What is the danger of returning True from __exit__?
  • How does contextlib.contextmanager translate a generator into a context manager?
Sharethis question

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