IPythonとPythonの違い

IPythonは、Pythonを対話型インタープリターを拡張したものだという認識は以前から知っていたのですが、何が異なるかの実際に調べました。

ここからは、IPythonにあってPythonにはない機能を紹介します。

補完機能

IPythonには補完機能があります。

In[2]: import mat

上記の状態でTabキーを押すと…

In[2]: import matplotlib

モジュールを出してくれます。非常に便利です。

複数ある場合は、その複数の候補を出してくれます。

イントロスペクション

モジュールやクラス、マジックコマンド(後述)に関して、使い方やソースコードを表示してくれます。

モジュール、クラス、マジックコマンドの後ろに?を置くとdocstring(使い方)、??を置くとソースコードが表示されます。

モジュールのdocstringやソースコードを見る場合は、importしてから実行します。

matplotlibのdocstringの一部(例)

Docstring:
::
  %matplotlib [-l] [gui]

Set up matplotlib to work interactively.

This function lets you activate matplotlib interactive support
at any point during an IPython session. It does not import anything
into the interactive namespace.

If you are using the inline matplotlib backend in the IPython Notebook
you can set which figure formats are enabled using the following::

    In [1]: from IPython.display import set_matplotlib_formats

    In [2]: set_matplotlib_formats('pdf', 'svg')

The default for inline figures sets `bbox_inches` to 'tight'. This can
cause discrepancies between the displayed image and the identical
image created using `savefig`. This behavior can be disabled using the
`%config` magic::

    In [3]: %config InlineBackend.print_figure_kwargs = {'bbox_inches':None}

In addition, see the docstring of
`IPython.display.set_matplotlib_formats` and
`IPython.display.set_matplotlib_close` for more information on
changing additional behaviors of the inline backend.

matplotlibのソースコードの一部(例)

Source:
    @skip_doctest
    @line_magic
    @magic_arguments.magic_arguments()
    @magic_arguments.argument('-l', '--list', action='store_true',
                              help='Show available matplotlib backends')
    @magic_gui_arg
    def matplotlib(self, line=''):
        """Set up matplotlib to work interactively.

        This function lets you activate matplotlib interactive support
        at any point during an IPython session. It does not import anything
        into the interactive namespace.

        If you are using the inline matplotlib backend in the IPython Notebook
        you can set which figure formats are enabled using the following::

            In [1]: from IPython.display import set_matplotlib_formats

            In [2]: set_matplotlib_formats('pdf', 'svg')

        The default for inline figures sets `bbox_inches` to 'tight'. This can
        cause discrepancies between the displayed image and the identical
        image created using `savefig`. This behavior can be disabled using the
        `%config` magic::

            In [3]: %config InlineBackend.print_figure_kwargs = {'bbox_inches':None}

        In addition, see the docstring of
        `IPython.display.set_matplotlib_formats` and
        `IPython.display.set_matplotlib_close` for more information on
        changing additional behaviors of the inline backend.

でIPythonに戻ります。

使いたいモジュール、クラス、マジックコマンドについてコードを手軽に確認できることは非常に便利です。

マジックコマンド

マジックコマンドは、ipythonで利用できるコマンドです。大きく2つに分かれます。

ラインマジック

最初に%があるコマンドを指します。以降をその1行の内容を引数としてコマンドに渡します。

例として実行時間を計測する%timeやpythonのスクリプトを実行する%runがあります。

セルマジック

最初に%%があるコマンドを指します。複数の行の内容を引数としてコマンドに渡します。

ほとんどがPython以外の言語の実行用コマンドです。

その他のマジックコマンドは %quickref で確認できます。

システムシェルを実行

最初に!を置くことでipython上でシステムシェルのコマンドを実行できます。

In[2]: !pwd
/home/hoge
In[3]:

まとめ

紹介したように、IPythonは、Pythonにはない機能がたくさんありました。

Pythonのコードで実行する場合でもこのような機能があればなぁと思いました。