『Python数値計算ノート』ではアフィリエイトプログラムを利用して商品を紹介しています。

【Jupyter Notebook】ヘルプ機能と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によるインポート可能なモジュール一覧

コメント