← Quiz: Context Managers: all 10 questions
Quiz: Context Managers · Question 7 of 10
In a @contextmanager function, where should the clean-up code go?
In a @contextmanager function, where should the clean-up code go?
- A.Before the yield
- B.After the yield, inside a finally block
- C.In a separate exit method
- D.Inside an except block only
Answer & explanation
Correct answer: After the yield, inside a finally block
finally makes sure clean-up runs even when the with block raises.
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 →