Skip to content

Commit 0139645

Browse files
committed
Add example with two context managers in a single with statement
1 parent 638af16 commit 0139645

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

tests/snippets/with.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11

2-
ls = []
32

43
class ContextManager:
54
def __enter__(self):
@@ -15,24 +14,29 @@ def __str__(self):
1514
ls.append(3)
1615
return "c'est moi!"
1716

17+
ls = []
1818
with ContextManager() as c:
1919
print(c)
20-
2120
assert ls == [1, 3, 2]
2221

23-
ls = []
2422
class ContextManager2:
2523
def __enter__(self):
26-
print('Entrada')
27-
ls.append(1)
24+
print('Ni hau')
25+
ls.append(4)
2826
return ls
2927

3028
def __exit__(self, exc_type, exc_val, exc_tb):
31-
ls.append(2)
32-
print('Wiedersehen')
29+
ls.append(5)
30+
print('Ajuus')
3331

32+
ls = []
3433
with ContextManager2() as c:
3534
print(c)
36-
assert c == [1]
35+
assert c == [4]
36+
assert ls == [4, 5]
3737

38-
assert ls == [1, 2]
38+
ls = []
39+
with ContextManager() as c1, ContextManager2() as c2:
40+
print(c1)
41+
assert c2 == [1, 4, 3]
42+
assert ls == [1, 4, 3, 5, 2]

0 commit comments

Comments
 (0)