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

回答編集履歴

2

docstringを追加

2021/05/13 11:17

投稿

ppaul
ppaul

スコア24672

answer CHANGED
@@ -1,5 +1,11 @@
1
1
  パッケージの中身って、どれも確認が可能なものなのでしょうか?
2
2
 
3
+ Pythonで書かれた部分は、簡単に確認が可能です。
4
+
5
+ 動的リンクライブラリの形で提供されているC言語やアセンブラで書かれた部分は、別途ソースコードをダウンロードすれば確認できます。
6
+
7
+ Pythonで書かれた部分の確認は以下のように行います。
8
+
3
9
  ```python
4
10
  >>> print(numpy)
5
11
  <module 'numpy' from 'C:\Users\myname\anaconda3\lib\site-packages\numpy\__init__.py'>
@@ -7,5 +13,92 @@
7
13
  というように場所を表示して、そこを見ればPythonで書かれた部分は簡単に確認できます。
8
14
 
9
15
  また、Pythonで書かれた関数などはinspectモジュールをimportすれば、inspect.getsource関数を使ってソースを表示することが出来ます。
16
+ 少々長いですが以下のような感じです。
10
17
 
18
+ ```python
19
+ >>> print(inspect.getsource(numpy.split))
20
+ @array_function_dispatch(_split_dispatcher)
21
+ def split(ary, indices_or_sections, axis=0):
22
+ """
23
+ Split an array into multiple sub-arrays as views into `ary`.
24
+
25
+ Parameters
26
+ ----------
27
+ ary : ndarray
28
+ Array to be divided into sub-arrays.
29
+ indices_or_sections : int or 1-D array
30
+ If `indices_or_sections` is an integer, N, the array will be divided
31
+ into N equal arrays along `axis`. If such a split is not possible,
32
+ an error is raised.
33
+
34
+ If `indices_or_sections` is a 1-D array of sorted integers, the entries
35
+ indicate where along `axis` the array is split. For example,
36
+ ``[2, 3]`` would, for ``axis=0``, result in
37
+
38
+ - ary[:2]
39
+ - ary[2:3]
40
+ - ary[3:]
41
+
42
+ If an index exceeds the dimension of the array along `axis`,
43
+ an empty sub-array is returned correspondingly.
44
+ axis : int, optional
45
+ The axis along which to split, default is 0.
46
+
47
+ Returns
48
+ -------
49
+ sub-arrays : list of ndarrays
50
+ A list of sub-arrays as views into `ary`.
51
+
52
+ Raises
53
+ ------
54
+ ValueError
55
+ If `indices_or_sections` is given as an integer, but
56
+ a split does not result in equal division.
57
+
58
+ See Also
59
+ --------
60
+ array_split : Split an array into multiple sub-arrays of equal or
61
+ near-equal size. Does not raise an exception if
62
+ an equal division cannot be made.
63
+ hsplit : Split array into multiple sub-arrays horizontally (column-wise).
64
+ vsplit : Split array into multiple sub-arrays vertically (row wise).
65
+ dsplit : Split array into multiple sub-arrays along the 3rd axis (depth).
11
- 動的リンクライブラリの形で提供されているC言語やアセンブラで書かれた部分は、別途ソースコードをダウンロードすれば確認できます。
66
+ concatenate : Join a sequence of arrays along an existing axis.
67
+ stack : Join a sequence of arrays along a new axis.
68
+ hstack : Stack arrays in sequence horizontally (column wise).
69
+ vstack : Stack arrays in sequence vertically (row wise).
70
+ dstack : Stack arrays in sequence depth wise (along third dimension).
71
+
72
+ Examples
73
+ --------
74
+ >>> x = np.arange(9.0)
75
+ >>> np.split(x, 3)
76
+ [array([0., 1., 2.]), array([3., 4., 5.]), array([6., 7., 8.])]
77
+
78
+ >>> x = np.arange(8.0)
79
+ >>> np.split(x, [3, 5, 6, 10])
80
+ [array([0., 1., 2.]),
81
+ array([3., 4.]),
82
+ array([5.]),
83
+ array([6., 7.]),
84
+ array([], dtype=float64)]
85
+
86
+ """
87
+ try:
88
+ len(indices_or_sections)
89
+ except TypeError:
90
+ sections = indices_or_sections
91
+ N = ary.shape[axis]
92
+ if N % sections:
93
+ raise ValueError(
94
+ 'array split does not result in an equal division')
95
+ return array_split(ary, indices_or_sections, axis)
96
+ ```
97
+
98
+ また、上記の関数のdocstringの部分は以下を実行すれば表示することができます。
99
+
100
+ ```python
101
+ print(numpy.split.__doc__)
102
+ または
103
+ help(numpy.split)
104
+ ```

1

inspect.getsourceを追加

2021/05/13 11:17

投稿

ppaul
ppaul

スコア24672

answer CHANGED
@@ -6,4 +6,6 @@
6
6
  ```
7
7
  というように場所を表示して、そこを見ればPythonで書かれた部分は簡単に確認できます。
8
8
 
9
+ また、Pythonで書かれた関数などはinspectモジュールをimportすれば、inspect.getsource関数を使ってソースを表示することが出来ます。
10
+
9
11
  動的リンクライブラリの形で提供されているC言語やアセンブラで書かれた部分は、別途ソースコードをダウンロードすれば確認できます。