context managers Flashcards

1
Q

context manager

A

with context_manager:
context_manager.__enter__()
body
context_manager.__exit__()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

context manager protocol

A
\_\_enter\_\_(self):   
\_\_exit\_\_(self,
     exc_type,
     exc_val,
     exc_tb
)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

__enter__() return …

A

context manager

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

default behaviour with exceptions in with block

A

__exit__() propagates exeptions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

to propagates exceptions from with block …

A

__exit__() must return False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

standart library for working with context managers

A

contextlib

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

context manager decorator

A
@contextlib.contextmanager
def my_context_manager():
    # 
    try:
        yield [value]
        # 
    except:
    raise

with my_context_manager() as x:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

multiple context managers

A

with cm1() as a, cm2() as b, … :

How well did you know this?
1
Not at all
2
3
4
5
Perfectly