
Pythonで新しいライブラリや関数を使うとき、あるいは使い方が思い出せないとき、その場でサッと調べられたら便利ですよね。そんな時に活躍するのが、Python標準のhelp()関数です。インターネットで検索しなくても、インタラクティブシェルで直接情報を得ることができます。
まずは、Pythonのインタラクティブシェルを起動して、試してみましょう。
# mathモジュール全体のヘルプを表示
import math
help(math)
# mathモジュール内の特定の関数(例:sqrt)のヘルプを表示
help(math.sqrt)
# 組み込み関数(例:print)のヘルプを表示
help(print)実行結果
Help on built-in module math:
NAME
math
DESCRIPTION
This module provides access to the mathematical functions
defined by the C standard.
FUNCTIONS
acos(x, /)
Return the arc cosine (measured in radians) of x.
The result is between 0 and pi.
acosh(x, /)
Return the inverse hyperbolic cosine of x.
asin(x, /)
Return the arc sine (measured in radians) of x.
The result is between -pi/2 and pi/2.
asinh(x, /)
Return the inverse hyperbolic sine of x.
atan(x, /)
Return the arc tangent (measured in radians) of x.
-- More --help()関数にモジュール名や関数名、クラス名などを渡すことで、そのオブジェクトに関する詳細なドキュメント(Docstring)が表示されます。これを見れば、引数の種類や戻り値、簡単な使用例などが一目で分かります。
みーちゃんのワンポイント
help()で表示されるドキュメントは、Qキーを押すことでいつでも終了できますよ。
