回答編集履歴

3

追記

2018/09/12 11:58

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  →(②この段階で*argsに(3,4)が代入されるようだがどういう理屈か?)~~~
8
8
 
9
- 0. 各printが表示される
9
+ 0. **aaaの実行に際して、内部の**各printが表示される
10
10
 
11
11
  0. 関数new_funciton**(関数オブジェクトaaa)**が最後にresultを返す=7 を返す
12
12
 

2

追記

2018/09/12 11:57

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -27,3 +27,73 @@
27
27
 
28
28
 
29
29
  良いアプローチだと思います。
30
+
31
+
32
+
33
+ ---
34
+
35
+ 数行増やしてみました。
36
+
37
+ ```Python
38
+
39
+ def decofunc(targetfunction):
40
+
41
+ print('decofunc is called')
42
+
43
+
44
+
45
+ def new_function(*args,**kwargs):
46
+
47
+ print('Runnning funciton is', targetfunction.__name__)
48
+
49
+ print('Positional arguments are', args)
50
+
51
+ print('Keyword arguments are', kwargs)
52
+
53
+ result = targetfunction(*args, **kwargs)
54
+
55
+ print('Result is', result)
56
+
57
+ return result
58
+
59
+
60
+
61
+ return new_function
62
+
63
+
64
+
65
+ def adding(a,b):
66
+
67
+ return a+b
68
+
69
+
70
+
71
+ aaa = decofunc(adding)
72
+
73
+ print('aaa is', aaa)
74
+
75
+ print(aaa(3,4))
76
+
77
+ ```
78
+
79
+
80
+
81
+ **実行結果** [Wandbox](https://wandbox.org/permlink/EupJb5nR6zJUu800)
82
+
83
+ ```plain
84
+
85
+ decofunc is called
86
+
87
+ aaa is <function decofunc.<locals>.new_function at 0x7ff5ae7fa840>
88
+
89
+ Runnning funciton is adding
90
+
91
+ Positional arguments are (3, 4)
92
+
93
+ Keyword arguments are {}
94
+
95
+ Result is 7
96
+
97
+ 7
98
+
99
+ ```

1

追記

2018/09/12 11:53

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -11,3 +11,19 @@
11
11
  0. 関数new_funciton**(関数オブジェクトaaa)**が最後にresultを返す=7 を返す
12
12
 
13
13
  0. 7が出力される
14
+
15
+
16
+
17
+ > 最終行に decofunc(adding(3,4)) という行を追加しても上記実行結果と同じものが出力されず
18
+
19
+
20
+
21
+ decofuncの返り値は関数オブジェクトだからです。
22
+
23
+
24
+
25
+ > デコレータ=高階関数の記法の一つということが分かったので高階関数を理解すればデコレータも理解できると思い、理解を掘り下げているとこです。
26
+
27
+
28
+
29
+ 良いアプローチだと思います。