class foo1: '''something code''' class foo2: '''something code''' class foo3: '''something code''' import foo import inspect classes = map(lambda x:x[0],inspect.getmembers(foo,inspect.isclass)) print classes # => ['foo1','foo2','foo3'] inspect.getmembers(object)はobjectに登録されているオブジェクトの全メンバーを取得します。 形式は[(member1,type),(member2,type),....]といった形。 inspect.is--はそのオブジェクトが--に一致するかを判定するもので、 class, module, fu
呼称: インスペクション機能の触り 目的: inspect モジュールの使用方法を学ぶ 特徴: ログ出力時に pprint モジュールを使うと整形されて見易い 用例: 実行中のソースの行番号を出力する、デバッグ 備考: inspect モジュールはもっと奥が深そう Python で実行中のソースの行番号を出力する仕組み(C 言語でいう __LINE__ マクロがあるかどうか)を調べてみたら、幾つかの方法が分かりました。sys._getframe を使う方法もありますが、ここでは inspect モジュールの使用方法を紹介します。 シンプルな方法では、以下で現在行を取得できます。 inspect.currentframe().f_lineno ある共通関数で、どこから呼び出されたか知りたい場合、以下で呼び出し元の行番号を取得できます。 inspect.currentframe().f_bac
みんなのIoT/みんなのPythonの著者。二子玉近く160平米の庭付き一戸建てに嫁/息子/娘/わんこと暮らしてます。月間1000万PV/150万UUのWebサービス運営中。 免責事項 プライバシーポリシー deliciousにsee()というのが上がっていたので早速ためしてみた。 see()というのは,簡単に言うとdir()より便利で気の利いたオブジェクトインスペクタだ。オブジェクトにどんなメソッドやアトリビュートがあるのかを簡単に調べることが出来るコマンドライクな関数。 たとえばPythonの文字列をsee()に与えてみるとこうなる。 In [1]:see('') Out[1]: [] in + * % < <= == != > >= hash() help() len() repr() str() .capitalize() .center() .count() .decode()
目次 dis/inspect モジュールと ceval.c を使った Python のハッキング dis モジュールと python interpreter 関数に属する func_code オブジェクトとco_varnames, co_names, co_consts 属性 python interpreter ceval.c:Python Virtual Machine の C ソース・コード inspect モジュール dis, inspect を使った一行コード python コードの hack x,y = y,x, x,z,y = z,y,x Python オブジェクト 関数 STORE_GLOBAL/STORE_NAME inspect 経由による クラスの disassemble LOAD_ATTRI リスト hash generator thread decorator 構
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く