Which two special methods must an object implement to be used as a context manager in a with statement?
Correct answer D. __enter__ and __exit__
Explanation
The with statement calls __enter__ on entry, binding its return value to the name after as, and calls __exit__ on exit with details of any exception. __init__ and __del__ handle construction and finalisation, while __iter__ and __next__ form the iterator protocol. There is no __open__ protocol.
Answer checked How we write and check questions
Discussion