ヘルプの表示と Tab 補完機能

ヘルプの表示と Tab 補完機能

help()関数

 Python のオブジェクトには docstring とよばれる説明文(ドキュメント)が付属していて、help()関数を使って呼び出すことができるようになっています。たとえば、help(abs) と記述すると、指定するべき引数や戻り値などが表示されます。

help(abs)
Help on built-in function abs in module builtins:

abs(x, /)
  Return the absolute value of the argument.

簡易記号によるヘルプの表示

 Jupyter Notebook では、? という簡易記号を使ってオブジェクトの情報にアクセスできるようになっています。

all?
Signature: all(iterable, /)
Docstring:
Return True if bool(x) is True for all values x in the iterable.

If the iterable is empty, return True.
Type: builtin_function_or_method

 組み込み以外のオブジェクトについて調べるときは、モジュールをインポートしてからヘルプ機能を使います。

import math

math.sqrt?

Docstring:
sqrt(x)

Return the square root of x.
Type: builtin_function_or_method

 Jupyter のマジックコマンドも docstring をもっているので、後ろに ? を添えるとヘルプを見ることができます。

%time?
Docstring:
Time execution of a Python statement or expression.

The CPU and wall clock times are printed, and the value of the
expression (if any) is returned. Note that under Win32, system time
is always reported as 0, since it can not be measured.

 ワイルドカードを使うこともできます。たとえば、strオブジェクトについて、"en" を含む属性やメソッドを探す場合は次のように入力します。

str.*en*?
str.encode
str.endswith
str.isidentifier

 

Tab補完

 オブジェクト名のあとにピリオドを添えて [Tab] キーを押すと、オブジェクトの属性やメソッドの一覧が表示されます。

 Jupyter Tabによるオブジェクトのメソッド検索

 ピリオドのあとに、途中の文字を入れてから [Tab] キーを押すと、候補が絞り込まれます。

 Jupyter Tabによるcで始まるメソッド検索

 import のあとに半角スペースを入れてから [Tab] キーを押すと、インポートできるモジュールの一覧が表示されます。

 Jupyter Tabによるインポート可能なモジュール一覧