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)- A. CM3 object
- B. None
- C. 42
- 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
- What is printed? python from contextlib import contextmanager @contextmanager def cm(): print("setup") yield 1 print("teardown") with cm()…
- What is printed? python class A: def enter(self): print("A enter"); return self def exit(self, a): print("A exit") class B: def…
- What is printed? python class A: def who(self): return "A" class B(A): def who(self): return "B" class C(A): def who(self): return "C"…
- What is printed? python class A: def f(self): return 1 class B(A): def f(self): return 2 + super().f() class C(B): def f(self): return 3 +…
- What is printed? python class F: @staticmethod def add(a, b): return a + b @classmethod def create(cls): return cls() print(F.add(2, 3)) f…
- What is printed? python class H: slots = ["a", "b"] h = H() h.a = 1 try: h.c = 2 except AttributeError: print("AttributeError") print(h.a)