回答編集履歴

2

関数の引数及び戻り値

2018/02/09 03:04

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -76,7 +76,7 @@
76
76
 
77
77
  ■余談
78
78
 
79
- python 3.5以降なら関数の戻り値の型を明示するとIDE(統合開発環境)でヒント情報や型不一致の警告を出してくれます。
79
+ python 3.5以降なら関数の引数及び戻り値の型を明示するとIDE(統合開発環境)でヒント情報や型不一致の警告を出してくれます。
80
80
 
81
81
  ```Python
82
82
 

1

サンプルコードを追加

2018/02/09 03:04

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -1,3 +1,85 @@
1
1
  [@functools.lru_cache](https://docs.python.jp/3/library/functools.html#functools.lru_cache)
2
2
 
3
3
  > 関数をメモ化用の呼び出し可能オブジェクトでラップし、最近の呼び出し最大 maxsize 回まで保存するするデコレータです。高価な関数や I/O に束縛されている関数を定期的に同じ引数で呼び出すときに、時間を節約できます。
4
+
5
+
6
+
7
+ 以下サンプルコード
8
+
9
+
10
+
11
+ ```Python
12
+
13
+ # -*- coding: utf8 -*-
14
+
15
+ import functools
16
+
17
+
18
+
19
+
20
+
21
+ @functools.lru_cache(maxsize=None)
22
+
23
+ def get_stopwords() -> tuple: # 上記のc2に該当
24
+
25
+ '''
26
+
27
+ ストップワードリストを返す
28
+
29
+ '''
30
+
31
+
32
+
33
+ from time import sleep
34
+
35
+ # キャッシュ確認用のデバックスリープ!
36
+
37
+ sleep(3)
38
+
39
+
40
+
41
+ import urllib.request
42
+
43
+ url = 'http://svn.sourceforge.jp/svnroot/slothlib/CSharp/Version1/SlothLib/NLP/Filter/StopWord/word/Japanese.txt'
44
+
45
+ with urllib.request.urlopen(url) as response:
46
+
47
+ stop_words = [w for w in response.read().decode().split('\r\n') if w != '']
48
+
49
+ stop_words.extend(['れる', 'られる', 'する', 'いる', 'なる', 'ある', 'できる', 'おる'])
50
+
51
+ return tuple(stop_words)
52
+
53
+
54
+
55
+
56
+
57
+ def main() -> None:
58
+
59
+ for i in range(10):
60
+
61
+ print(get_stopwords())
62
+
63
+
64
+
65
+
66
+
67
+ if __name__ == '__main__':
68
+
69
+ main()
70
+
71
+
72
+
73
+ ```
74
+
75
+
76
+
77
+ ■余談
78
+
79
+ python 3.5以降なら関数の戻り値の型を明示するとIDE(統合開発環境)でヒント情報や型不一致の警告を出してくれます。
80
+
81
+ ```Python
82
+
83
+ def get_stopwords() -> tuple:
84
+
85
+ ```