← Quiz: Context Managers: all 10 questions
Quiz: Context Managers · Question 4 of 10
What does this print? (code)
What does this print?
class C:
def __enter__(self):
print("in")
return self
def __exit__(self, *a):
print("out")
with C():
print("body")
- A.in out body
- B.in body out
- C.body in out
- D.in body
Answer & explanation
Correct answer: in body out
enter runs first, then the body, then exit.
Can you get the rest right without peeking?
Take the timed Quiz: Context Managers quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →