context managers Flashcards
1
Q
context manager
A
with context_manager:
context_manager.__enter__()
body
context_manager.__exit__()
2
Q
context manager protocol
A
\_\_enter\_\_(self): \_\_exit\_\_(self, exc_type, exc_val, exc_tb )
3
Q
__enter__() return …
A
context manager
4
Q
default behaviour with exceptions in with block
A
__exit__() propagates exeptions
5
Q
to propagates exceptions from with block …
A
__exit__() must return False
6
Q
standart library for working with context managers
A
contextlib
7
Q
context manager decorator
A
@contextlib.contextmanager def my_context_manager(): # try: yield [value] # except:
raise
with my_context_manager() as x:
8
Q
multiple context managers
A
with cm1() as a, cm2() as b, … :