What does the following code output? from contextlib import contextmanager @contextmanager def managed(): print(‘enter’) try: yield 42 finally: print(‘exit’) with managed() as v: print(v)
Python ProfessionalHard
Python Professional — Hard
What does the following code output? from contextlib import contextmanager @contextmanager def managed(): print(‘enter’) try: yield 42 finally: print(‘exit’) with managed() as v: print(v)
Explanation
The correct output is "enter 42 exit" because the context manager function "managed()" prints "enter" before yielding 42, then prints "exit" after the yield statement. The value 42 is printed when the context manager is used with the "with" statement.