回答編集履歴
1
追記
test
CHANGED
@@ -27,3 +27,67 @@
|
|
27
27
|
|
28
28
|
|
29
29
|
そのケースでは、returnされたwrapperがそのままhelloに束縛されます。
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
---
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
helloをprintすると意味がつかみやすいかもしれません。
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
```python
|
42
|
+
|
43
|
+
def decorator(func):
|
44
|
+
|
45
|
+
def wrapper(*args, **kwargs):
|
46
|
+
|
47
|
+
print(func.__name__ + "を実行します")
|
48
|
+
|
49
|
+
func(*args, **kwargs)
|
50
|
+
|
51
|
+
return wrapper
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
@decorator
|
56
|
+
|
57
|
+
def hello(name="someone"):
|
58
|
+
|
59
|
+
print("Hello", name, sep=" ")
|
60
|
+
|
61
|
+
print(hello) # デコレータで作ったhello
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
def hello(name="someone"):
|
66
|
+
|
67
|
+
print("Hello", name, sep=" ")
|
68
|
+
|
69
|
+
hello = decorator(hello)
|
70
|
+
|
71
|
+
print(hello) # 「だいたい同じ」方法で作ったhello
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
hello()
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
""" =>
|
80
|
+
|
81
|
+
<function decorator.<locals>.wrapper at 0x7ff76c22c158>
|
82
|
+
|
83
|
+
<function decorator.<locals>.wrapper at 0x7ff76c2c4840>
|
84
|
+
|
85
|
+
helloを実行します
|
86
|
+
|
87
|
+
Hello someone
|
88
|
+
|
89
|
+
"""
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
```
|