Advanced Python · interview question
Which option is the safer default when shipping code around class-based context managers?
Multiple choiceHardchapter-14-curatedpremiumadvanced-pythonclass-based-context-managers
class ConnectionManager:
def __enter__(self):
return self
def __exit__(self, exc_type, exc, tb):
print("closing")Answer and explanation
Correct answer
C. Choose the simpler shape that keeps the rule obvious, validated, and easy to observe in logs or tests.
Why
The safest default is usually the one that lowers ambiguity and makes errors easier to diagnose.
Test yourself on Advanced Python
A free test with instant, explained results. No account needed.
More Advanced Python questions
- Which move is the best default when functools.wraps becomes part of this workflow?
- What do interviewers usually hear as a strong signal when you discuss decorator factories well?
- Which explanation would sound strongest in an interview when discussing functools.wraps?
- Which explanation would sound strongest in an interview when discussing contextlib helpers?
- What assumption should be made explicit when working with contextlib helpers?
- Which option treats decorator factories as an engineering policy decision instead of a syntax trick?