回答編集履歴
1
decoratorについて
answer
CHANGED
@@ -16,4 +16,47 @@
|
|
16
16
|
# --start--
|
17
17
|
# Hello Decorator - master
|
18
18
|
# --end--
|
19
|
+
```
|
20
|
+
|
21
|
+
----
|
22
|
+
@wrapsなしだと何をwrapしたのか分からなくなってしまう問題があります。
|
23
|
+
|
24
|
+
```Python
|
25
|
+
def deco(func):
|
26
|
+
def wrapper(*args, **kwargs):
|
27
|
+
print('--start--')
|
28
|
+
func(*args, **kwargs)
|
29
|
+
print('--end--')
|
30
|
+
return wrapper
|
31
|
+
|
32
|
+
@deco
|
33
|
+
def test(name):
|
34
|
+
"""How to use"""
|
35
|
+
print(f'Hello Decorator - {name}')
|
36
|
+
|
37
|
+
print(test.__doc__)
|
38
|
+
# None
|
39
|
+
print(test)
|
40
|
+
# <function deco.<locals>.wrapper at 0x102dc38c8>
|
41
|
+
```
|
42
|
+
|
43
|
+
@wrapsを使うと、もとのオブジェクトの属性を引き継ぐことができます。
|
44
|
+
```Python
|
45
|
+
def deco(func):
|
46
|
+
@wraps
|
47
|
+
def wrapper(*args, **kwargs):
|
48
|
+
print('--start--')
|
49
|
+
func(*args, **kwargs)
|
50
|
+
print('--end--')
|
51
|
+
return wrapper
|
52
|
+
|
53
|
+
@deco
|
54
|
+
def test(name):
|
55
|
+
"""How to use"""
|
56
|
+
print(f'Hello Decorator - {name}')
|
57
|
+
|
58
|
+
print(test.__doc__)
|
59
|
+
# How to use
|
60
|
+
print(test)
|
61
|
+
# <function test at 0x102e98488>
|
19
62
|
```
|