teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

修正

2020/06/26 00:19

投稿

LouiS0616
LouiS0616

スコア35678

answer CHANGED
@@ -4,47 +4,39 @@
4
4
 
5
5
  @contextmanager
6
6
  def spam1():
7
- print('spam')
8
7
  try:
9
8
  print('try')
10
9
  yield
11
10
  finally:
12
11
  print('finally')
13
-
14
- print('return')
15
12
 
16
13
  @contextmanager
17
14
  def spam2():
18
- print('spam')
19
15
  print('try')
20
16
  yield
21
17
  print('finally')
22
- print('return')
18
+
23
19
 
20
+ for spam in [spam1, spam2]:
24
- print('-'*48)
21
+ print('-'*48)
25
- try:
22
+ try:
26
- with spam1():
23
+ with spam():
24
+ print('with')
27
- raise Exception
25
+ raise Exception
28
- except Exception:
26
+ except Exception:
29
- print('exception')
27
+ print('exception')
30
-
31
- print('-'*48)
28
+
32
- try:
33
- with spam2():
34
- raise Exception
35
- except Exception:
36
- print('exception')
37
29
  ```
38
30
 
39
- **実行結果** [Wandbox](https://wandbox.org/permlink/p9FeinaOmPsQ1hX1)
31
+ **実行結果** [Wandbox](https://wandbox.org/permlink/HLq8S1n1RIzmt0bV)
40
32
  ```
41
33
  ------------------------------------------------
42
- spam
43
34
  try
35
+ with
44
36
  finally
45
37
  exception
46
38
  ------------------------------------------------
47
- spam
48
39
  try
40
+ with
49
41
  exception
50
42
  ```