Python Code Readingの後の懇親会で少し話題になっていたので公開。
ipythonで、便利だと思う機能の列挙
1. 補完
タブキーで補完してくれる。まぁ、これは誰でも知ってるだろう。
2. コマンドを実行できる
!を使えば、Pythonの外のプログラムと連携が可能。それをPythonの変数に入れることも可能。
In [12]: !free total used free shared buffers cached Mem: 2075736 1032976 1042760 0 116104 711628 -/+ buffers/cache: 205244 1870492 Swap: 738948 0 738948 In [13]: a = !free == [' total used free shared buffers cached', 'Mem: 2075736 1033420 1042316 0 116104 711628', '-/+ buffers/cache: 205688 1870048', 'Swap: 738948 0 738948'] In [14]: a[1] Out[14]: 'Mem: 2075736 1033420 1042316 0 116104 711628'
3. ?をつかってオブジェクトの情報が見える。
obj? でいろいろ見える。 obj?? までやると、もっといろいろ見える。関数だったら、関数のソースコードが見えたりする。
magic(%edとか)についても、?をつけるとヘルプが見える。
In [15]: a? Type: SList Base Class: <class 'IPython.genutils.SList'> String Form: [' total used free shared buffers cached', 'Mem: 2075736 <...> 711628', '-/+ buffers/cache: 205688 1870048', 'Swap: 738948 0 738948'] Namespace: Interactive Length: 4 Docstring: List derivative with a special access attributes. These are normal lists, but with the special attributes: .l (or .list) : value as list (the list itself). .n (or .nlstr): value as a string, joined on newlines. .s (or .spstr): value as a string, joined on spaces. .p (or .paths): list of path objects Any values which require transformations are computed only once and cached.
4. %ed を使って、外部エディタでコードをかける。
インタラクティブシェルで、関数を作ってみて、ミスを見つけたとき、修正が面倒。%edを使ってファイルを外部エディタで編集可能&編集終了すると自動でロードしてくれる。
In [24]: %ed IPython will make a temporary file named: /tmp/ipython_edit_0BGpOd.py Editing... done. Executing edited code... ## < ここでVimが開いて、適当に関数を定義したりする。 Out[24]: 'def foo():\n print "foo"\n' In [25]: foo() foo