ProgrammingHardAccenture pattern

What is printed?

What is printed?
class CM3:
    def __enter__(self):
        return 42
    def __exit__(self, et, ev, tb):
        return False
with CM3() as val:
    print(val)
  1. A. CM3 object
  2. B. None
  3. C. 42
  4. D. True

Correct answer: C. 42

The value bound by 'as' is whatever __enter__ returns, not the context manager instance itself; here __enter__ returns 42, so val is 42.

More Python Programming questions

Exams that ask this topic