このサイトについて

Pythonのオブジェクトインスペクト関数「see()」

Pythonのオブジェクトインスペクト関数「see()」

deliciousにsee()というのが上がっていたので早速ためしてみた。

see()というのは,簡単に言うとdir()より便利で気の利いたオブジェクトインスペクタだ。オブジェクトにどんなメソッドやアトリビュートがあるのかを簡単に調べることが出来るコマンドライクな関数。

たとえばPythonの文字列をsee()に与えてみるとこうなる。

In [1]:see('')
Out[1]:
[] in + * % < <= == != > >= hash()
help() len() repr() str() .capitalize() .center()
.count() .decode() .encode() .endswith() .expandtabs()
.find() .index() .isalnum() .isalpha() .isdigit()
.islower() .isspace() .istitle() .isupper() .join()
.ljust() .lower() .lstrip() .partition() .replace()
.rfind() .rindex() .rjust() .rpartition() .rsplit()
.rstrip() .split() .splitlines() .startswith() .strip()
.swapcase() .title() .translate() .upper() .zfill()

演算子や呼び出し可能オブジェクトをよりHuman Readableに変換してくれているのが分かる。

ワイルドカードを使った絞り込みなどもできる。

In [2]:see('', '.is*')
Out[2]:
.isalnum() .isalpha() .isdigit() .islower() .isspace()
.istitle() .isupper()

Pythonにはhelp()関数があって,オブジェクトを与えると英文の解説を表示してくれるけど,help()より冗長でなくなよいかんじ。またIPythonを使えばアトリビュートの補完ができるけど,演算子とかメソッドをそれと分かるように表示してくれるという点でsee()の方が分かりやすい。

素のままでは,see()を使う前に「from see import see」ってやらなければならないんだけど,ReadmeにはPythonのインタラクティブシェルやIPythonでsee()をグローバル関数みたいに予めimportしておく設定が書いてある。早速IPythonで読み込むようにしてみたけど,けっこう便利だ:-)。

2010-08-27 04:52