Practice mode Python

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.