Python Developer Interview Question

How do context managers work, and when have you written your own?

What the interviewer is probing, how to structure your answer, and a spoken example you can adapt.

Quick answer

A context manager is any object with dunder enter and dunder exit. The with statement calls enter, binds its return value to the as target, and guarantees exit runs afterward even if the body raises. Exit receives the exception details and can suppress the exception by returning True. The contextlib.contextmanager decorator lets you write the same thing as a generator with a single yield.

Why interviewers ask this

Resource leaks are one of the most common production failures in Python services, so interviewers check that you reach for with by reflex and understand the guarantee it provides. They also want to see that you can write one rather than only consume them, and that you know exit runs on the exception path, which is what makes it a cleanup mechanism rather than convenient syntax.

How to structure your answer

  • Name the two dunder methods and their jobs.
  • Stress that exit runs even on exceptions.
  • Mention the contextlib generator shortcut.
  • Describe one you wrote and the leak it prevented.

Example answer

Spoken example, first person

The protocol is two methods. Enter runs setup and returns whatever you want bound after the as keyword, and exit runs teardown. The important part is that exit runs whether the block finished cleanly or blew up, and it gets the exception type, value, and traceback so it can decide what to do. Returning True from exit swallows the exception, which I use sparingly because it hides failures. Most of the ones I write use contextlib.contextmanager, where the code before yield is setup, the code after is teardown, and I wrap the yield in try finally so cleanup still happens on an error. I built one at a fintech for database transactions that also stamped a request id onto the logging context, so every query inside the block was traceable and the id was cleared on the way out. Before that we were setting and clearing it by hand, and an early return in one handler leaked the id into the next request on the same worker.

Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.

See how it works

Follow-up questions to expect

  • How do you handle several context managers at once?
  • What does contextlib.ExitStack solve?
  • How would you write an async context manager?

Related python developer questions

Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.

Predict my questions

Rehearse the hard questions before they are asked

Practise with a live copilot, then walk in ready. A $29 Session Pass gets you through the interview with no subscription and no lock-in.

Get GhostPilot