Skip to content
Tech Interview Prep home
Technical interview guide

Decorators & Context Managers

Wrapping a function's behavior without changing its code, and guaranteeing setup/teardown runs even when something fails.

Read
47 min
Practice MCQs
25
Interview QA
25
Edition
v2
Editorial status
Reviewed

Scope: Python 3.14 decorator, context manager, contextlib, and asynchronous context manager semantics.

Interview QA

Treat each question like a live interview question: answer out loud first (structure, assumptions, tradeoffs), then open the model answer to spot gaps and rehearse a tighter follow-up.

Curated: · Written: · Reviewed:

QA-1

Explain decorator evaluation, application, and call-time execution as separate phases.

QA-2

Derive the effective call order of stacked authorization, caching, and logging decorators.

QA-3

How do closure cells and late binding affect decorator factories?

QA-4

Why does `functools.wraps` matter beyond cosmetic metadata?

QA-5

Design argument forwarding for a wrapper that consumes one configuration keyword.

QA-6

Implement and explain the three layers of a parameterized decorator.

QA-7

How would you preserve semantics when decorating both sync and async callables?

QA-8

Audit the lifecycle and concurrency risks of a stateful decorator object.

QA-9

Explain descriptor binding when decorators are applied to methods.

QA-10

Compare class decorators with metaclasses and simple registration.

QA-11

Design a retry decorator with sound exception and idempotency boundaries.

QA-12

Translate the context-manager protocol into entry, body, and exit steps.

QA-13

How would you roll back resources acquired before `__enter__` fails?

QA-14

Define a narrow and reviewable exception-suppression contract.

QA-15

Explain how exceptions are delivered through a generator context manager.

QA-16

Differentiate single-use, reusable, and reentrant context managers.

QA-17

When does `contextlib.suppress` improve code, and when does it hide defects?

QA-18

Compare native resource managers with `closing` and `aclosing` adapters.

QA-19

Use `nullcontext` to model optional ownership without duplicating a code block.

QA-20

Design dynamic all-or-nothing resource acquisition with `ExitStack`.

QA-21

Explain exception replacement and suppression during ExitStack unwinding.

QA-22

Manage mixed synchronous and asynchronous resources with `AsyncExitStack`.

QA-23

Design commit, rollback, and exception chaining for a transaction manager.

QA-24

Why does a lock context manager prevent leaks but not deadlocks or races?

QA-25

Choose between a decorator and context manager for a scoped production concern.